2010-06-07 64 views
11

我試圖將子域名與symfony中的客戶ID進行匹配。Symfony動態子域名

即我已經customer1.example.com和customer2.example.com

域是存儲在表中。

當用戶轉到customer1.example.com時,我想要獲取子域名,在數據庫中查找域名,一旦匹配,就會爲該客戶部署應用配置,然後存儲customer_Id在全球屬性中,所以我確切知道我通過整個應用程序處理哪個客戶。虛擬主機將具有相關的通配符服務器名稱。

您是否設法實現了這一目標?如果是,如何實現?如果沒有,任何想法都將非常有幫助!

我正在考慮使用過濾器來做到這一點。

:-)

+1

+1好問題:-) – richsage 2010-06-07 13:16:12

+0

謝謝!希望有人知道答案! :-) – Flukey 2010-06-07 14:45:14

回答

3

也將需要喲將您的域設置爲通配符域,如果不是,您將需要爲每個客戶端手動創建每個子域。

另一種解決方案是不那麼依賴於交響樂使用的.htaccess

<IfModule mod_rewrite.c> 
    Options +FollowSymLinks 
    Options +Indexes 
    RewriteEngine On 
    RewriteBase/
    RewriteCond %{HTTP_HOST} !www.domain.com$ [NC] 
    RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).domain.com [NC] 
    RewriteRule (.*) $1?sub=%2&page=$1&domain=%{HTTP_HOST} [QSA,L] 
<IfModule> 

代碼基本上會發送到請求頁面的子域,域和請求的頁面。然後在PHP中,你可以檢查它是否等於你的客戶端用戶名。並允許您同時爲您的客戶使用託管域名。

我希望它有幫助。

0

因爲要加載不同的應用程序,過濾器也無濟於事。只需使用frontcontroller(index.php)來提取子域,如果app目錄存在,則加載應用程序(否則404)。您甚至可以將該ID存儲在應用程序配置中。

+0

嘿馬雷克。我沒有部署一個不同的應用程序,我只想使用sudomain作爲參數變量。 例如:我不想擁有http://example.com/company1/admin,我想要http://company1.example.com/admin 同樣的應用程序爲每個客戶,但我使用子域獲取公司相關數據。 昨晚我成功了。我創建了一個過濾器(domainMatchFilter.class.php)。 – Flukey 2010-06-08 07:32:47

+0

我抓住了子域,在客戶表上搜索匹配域(表中的域域有一個索引)。一旦找到它,就會從結果集中爲客戶對象設置一個配置變量。即sfConfig :: set(「customer」,$ customer)。 如果找不到客戶,它會重定向到主頁(example.com)。你認爲重定向到一個說這個客戶不存在的頁面會更好嗎? 這個過濾器運行在每個頁面請求上,它有點慢,因爲它每次都查詢數據庫,但是,這是我知道如何去做的唯一方法。 – Flukey 2010-06-08 07:33:08

+0

我希望它像uservoice.com一樣工作。你註冊,你會得到自己的域名,即company1.uservoice.com等,這比uservoice.com/company1 – Flukey 2010-06-08 07:36:02

0

我正在做類似的事情。請注意,我沒有嘗試過這個確切的設置。

$tokens = explode('.', $_SERVER['SERVER_NAME'], 2); 
$app = $tokens[0] == 'www' ? 'default' : $tokens[0]; //assumes you aren't allowing www.app.example.com, change if you are 

try 
{ 
    $appConfiguration = ProjectConfiguration::getApplicationConfiguration($app, 'prod', false); 
} 
catch(InvalidArgumentException $e) //thrown if app doesn't exist 
{ 
    $fallbackConfiguration = ProjectConfiguration::getApplicationConfiguration('default', 'prod', false); 
    $context = sfContext::createInstance($fallbackConfiguration); 
    $request = $context->getRequest(); 
    $request->setParameter('module', 'default'); //set what route you want an invalid app to go to here 
    $request->setParameter('action', 'invalidApplication'); 
    $context->dispatch(); 
} 
if (isset($appConfiguration)) 
{ 
    sfContext::createInstance($appConfiguration)->dispatch(); 
} 
0

看看sfDomainRoutePlugin - 它做你想做的。但是,在當前版本中,您不會獲得Propel或DoctrineRoute功能,這意味着您必須根據插件返回的子域參數手動查找客戶。例如:

應用程序/前端/配置/ routing.yml中

# pick up the homepage 
homepage: 
    url:  /
    class:  sfDomainRoute 
    param:  { module: homepage, action: index } 
    requirements: 
    sf_host: [www.example.com, example.com] 

# catch subdomains for customers 
customer_subdomain: 
    url:  /
    class:  sfDomainRoute 
    param:  { module: customer, action: index } 

應用程序/前端/模塊/客戶/的actions.class.php

public function executeIndex(sfWebRequest $request) 
{ 
    // get the subdomain parameter 
    $this->subdomain = $request->getParameter('subdomain'); 
    // retrieve customer (you have to create the retrieveBySubdomain method) 
    $this->customer = CustomerPeer::retrieveBySubdomain($this->subdomain); 
} 

這僅僅是一個例子,但是我我自己使用一種類似的方法,插件做廣告。祝你好運。

如果您喜歡冒險,yuo可以參考「更多與symfony書籍」中的第2章。這會幫助你理解sfDomainRoutePlugin中的代碼。