2012-11-07 48 views
1

我正在構建一個小應用,人們可以在其中創建某種投資組合。在CakePHP中設置一個取決於ServerName的變量

目前每個用戶都可以使用url www.example.com/USERNAME鏈接到他們的投資組合,並處理在CakePHP的一切,這是我使用的路由器規則:

Router::connect('/:user/:action/*', array('controller'=>'pages'), array('named' => array('user'))); 

所以鏈接形成這樣:www.example.com/USERNAME/homewww.example.com/USERNAME/contact

這是我的問題。我希望用戶可以將其域的DNS指向我的IP,並更改www.mypersonalsite.com/homewww.mypersonalsite.com/contact等的URL。

就像tumblr一樣......

什麼是最好的方法來實現這一目標?

謝謝大家:)

+0

你不能用一個路徑的記錄指向一個域(如'www.e xample.com/username')。您只能將其指向域名,例如「www.example.com」或「username.example.com」。我建議重新考慮你的路由並改用子域名。這是另外一個問題,但在我確定之前已經回答了。這裏還有一個簡短的例子:http://book.cakephp.org/2.0/en/appendices/new-features-in-cakephp-2-0.html#router – jeremyharris

回答

0

添加一個新的領域,以用戶表「personal_domain」

確保您的Apache是​​這樣設置的網站,可爲您的應用程序將轉發點擊服務器到所有域你的蛋糕應用

在app_controller添加這樣的事情beforeFilter()

$domain = $_SERVER[ 'SERVER_NAME' ]; 
$sessionDomain = ''; 
if($this->Session->check('User.personal_domain')) 
{ 
    $sessionDomain = $this->Session->read('User.personal_domain'); 
} 

if($domain != $sessionDomain) 
{ 
    $User = ClassRegistry::init('User'); 
    $domainUser = $User->find('first', array(
     'conditions' => array(
      'User.personal_domain' => $domain 
     ) 
    )); 

    if(empty($domainUser)) 
    { 
     // whatever you usually do for users that don't have their own domain 
    } 
    else 
    { 
     $this->Session->write('User.personal_domain', $domainUser [ 'User' ][ 'personal_domain' ]); 
     // whatever you usually do, and now you know which user is connecting 
    // you should use this info with whatever auth method you use to make sure 
    // only that user can login to that domain 
    } 
} 
相關問題