2012-11-03 23 views
0

我們有一個在symfony 1.4框架中開發的網站。該網站應該能夠擁有多個域名。每個域名都有其特殊的主頁和其他所有內容。實際上,域必須是針對每個操作的參數,根據它,操作從數據庫獲取數據並顯示它。如何爲多個域配置Symfony 1.4項目?

例如,我們有一個關於我們的頁面。我們將about_us內容保存在about_us表中。此表有一個website_id。我們將網站信息保存在網站表格中。假設這樣的:

website (id, title, domain) 
about_us (id, content, website_id) 

網站內容:

(1, 'foo', 'http://www.foo.com') and (2, 'bar', 'http://www.bar.com') 

about_us內容:

(1, 'some foo', 1) and (2, 'some bar', 2) 

的問題是,我應該如何配置我的Symfony的項目,能夠做到這樣嗎?將域名作爲參數並在Symfony操作中使用?

+1

此外,看一看一個[這個問題](http://stackoverflow.com/questions/3521049/configure-symfony-project-for-multiple-domains)和[此酮](HTTP:/ /stackoverflow.com/questions/2989528/symfony-dynamic-subdomains)。 – j0k

+0

@ j0k謝謝,我之前看到過這個問題,但我的問題不同。 – samra

回答

1

您可以創建自己的路線類,擴展sfRoute。這條路線將一個 '域' 參數添加到所有請求:

//apps/frontend/lib/routing/myroute.class.php 

class myRoute extends sfRoute 
{ 

    public function matchesUrl($url, $context = array()) 
    { 
     // first check if it is a valid route: 
     if (false === $parameters = parent::matchesUrl($url, $context)) 
     { 
      return false; 
     } 

     $domain = $context['host']; 

     // add the $domain parameter: 
     return array_merge(array(
      'domain' => $domain 
      ), $parameters); 
    } 
} 

的routing.yml(例如):

default_module: 
    class: myRoute 
    url: /:module/:action/:id 
    ... 

在你的行動,你得到的與域名:

$request->getParameter('domain'); 
+0

謝謝!這正是我想要的 – samra

1

有很多方法可以做到這一點。 您可以擴展sfFrontWebController,並在dispatch()方法內添加額外的代碼。

# app/myapp/config/factories.yml 
all: 
    controller: 
    class: myController 


// lib/myController.class.php 
class myController extends sfFrontWebController 
{ 
    public function dispatch() 
    { 
     $selectedSite = SiteTable::retrieveByDomain($_SERVER['HTTP_HOST']); // Example 

     if (!$selectedSite) { 
      throw new sfException('Website not found'); 
     } 

     // Store any site value in parameter 
     $this->context->getRequest()->setParameter('site_id',$selectedSite->getId()); 

     parent::dispatch(); 
    } 
} 
+0

謝謝,我認爲這也會起作用。 – samra