2014-11-22 104 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