How to create a sub-domain in CakePHP?

前端 未结 3 1551
渐次进展
渐次进展 2021-01-06 04:59

CakePHP version-2.5.5

My domain name is http://www.thechatfun.com

Profile page link - http://www.thechatfun.com/users/profile

Chat page

相关标签:
3条回答
  • 2021-01-06 05:27

    profile.thechatfun.com and www.chat.thechatfun.com are different domains. It's possible for a single http server to handle both of these domains, but it won't happen automatically.

    Assuming your webserver is Apache, you will first need to configure the webserver to handle these domains correctly. You can add a VirtualHost directive so that both of these domains are handled by the same Virtual Host and share a document root, or you can add a Virtual Host for each and have separate document root directories for each domain.

    Your webserver first receives the HTTP request, then passes the request to PHP for processing. So, if your webserver isn't configured properly to handle these domains, you won't have the ability to control this in PHP or CakePHP.

    0 讨论(0)
  • 2021-01-06 05:31

    As long as you can configure your domain records to point both chat and profile subdomains to your server then you can change the htaccess file in your webroot folder and add..

        <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>
    

    I have this exact requirement and this works for me.

    0 讨论(0)
  • 2021-01-06 05:34

    Follow your context, inside this directory: /lib/Cake/Routing/Route , create file SubdomainRoute.php with content:

    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;
        }
    }
    



    When creating links you could do the following to make links pointing at other subdomains.

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


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

    0 讨论(0)
提交回复
热议问题