2014-11-22 103 views
4

的CakePHP版本2.5.5如何在CakePHP中創建子域?

我的域名是http://www.thechatfun.com

概況頁鏈接 - http://www.thechatfun.com/users/profile

聊天室網頁鏈接 - http://www.thechatfun.com/chats/index

上面兩個環節,我想看起來像http://profile.thechatfun.comhttp://www.chat.thechatfun.com

我無法在CakePHP中創建子域。

請幫我

感謝 ChatFun

+0

我認爲本教程對您有所幫助:http://book.cakephp.org/2.0/en/appendices/new-features-in-cakephp-2-0.html#routes-can-return-full -urls – 2014-11-23 04:24:39

+0

檢查它... http://stackoverflow.com/questions/6744733/how-to-create-sub-domains-using-cake-php – 2014-11-27 18:00:40

回答

1

按照你的背景下,這個目錄裏面:/lib/Cake/Routing/Route,創建文件SubdomainRoute.php的內容:

class SubdomainRoute extends CakeRoute { 

    public function match($params) { 
     $subdomain = isset($params['subdomain']) ? $params['subdomain'] : null; 
     unset($params['subdomain']); 
     $path = parent::match($params); 
     if ($subdomain) { 
      $path = 'http://' . $subdomain . '.thechatfun.com' . $path; 
     } 
     return $path; 
    } 
} 



當創建你可以鏈接請執行以下操作使鏈接指向其他子域。

echo $this->Html->link(
    'Profile', 
    array('subdomain' => 'profile', 'controller' => 'Users', 'action' => 'profile') 
); 

echo $this->Html->link(
    'Chats', 
    array('subdomain' => 'chat', 'controller' => 'Chats', 'action' => 'index') 
); 


參考:http://book.cakephp.org/2.0/en/appendices/new-features-in-cakephp-2-0.html#routes-can-return-full-urls

+0

我在哪裏可以調用SubdomainRoute? – gonzo 2015-02-23 22:25:38

1

profile.thechatfun.com和www.chat.thechatfun.com是不同的域。單個http服務器可以處理這兩個域,但它不會自動發生。

假設您的Web服務器是Apache,您首先需要配置Web服務器以正確處理這些域。您可以添加VirtualHost指令,以便這兩個域由同一個虛擬主機處理並共享文檔根目錄,或者可以爲每個虛擬主機添加一個虛擬主機,併爲每個域具有單獨的文檔根目錄。

您的網絡服務器首先收到HTTP請求,然後將請求傳遞給PHP進行處理。因此,如果您的網絡服務器配置不正確,無法處理這些域,您將無法在PHP或CakePHP中控制此功能。

3

只要你可以配置你的域名記錄指向兩個聊天和子域配置文件到您的服務器,那麼你可以改變htaccess文件在您的根目錄文件夾並添加..

<IfModule mod_rewrite.c> 
     #standard cake htaccess stuff 
     ... 

     RewriteCond %{HTTP_HOST} ^profile\.thechatfun\.com$ [NC] 
     RewriteRule ^(.*)$ http://www.thechatfun.com/users/profile/$1 [R=301,L] 

     RewriteCond %{HTTP_HOST} ^chat\.thechatfun\.com$ [NC] 
     RewriteRule ^(.*)$ http://www.thechatfun.com/chats/index/$1 [R=301,L] 

    </IfModule> 

我有這個確切的要求這對我有用。

+0

哪個'.htaccess'文件我將添加此代碼?在cakephp中有3個.htaccess。應用程序文件夾之外。在應用程序文件夾和webroot文件夾中。 – Chinmay235 2015-12-01 05:05:17

+0

@Chinu編輯'app_folder/.htaccess'文件,而不是'webroot/.htaccess' – 2016-06-11 03:40:15

+0

@NguyễnAnhTuấn感謝您長時間評論。 – Chinmay235 2016-06-13 06:38:43