Change CakePHP default routing based on URL

15 May 2011

Posted in CakePHP

CakePHP is a great framework, we can bake our apps and make it running in no time. Even though the official documentation not as good as CodeIgniter, but we can get many references out there that can help us. And not to mention they have series of video tutorial on their CakePHP TV site.

Let say I want to create an apps like Tumblr where users can register and use sub-domain for their account, but I don't want to create separate apps for this. And for sure I don't want to create the sub-domains manually in my cPanel every time someone register through my apps.

What we can do is to change default routing for these users based on their sub-domain. If we can detect sub-domain in the URL, then we load blogs controller, and if it doesn't, just load our pages controller.

Wildcard sub-domain

To do this, we need to set wildcard sub-domain on our server. Point it to our main domain name. There are so many articles for this and I won't to explain it here, ask our beloved uncle Google.

The flow

These a basic flow what we want to do in our apps.

  • If user access using http://example.com or http://www.example.com, we load our pages controller.
  • If user access using http://user.example.com or http://syahzul.example.com, we load our blogs controller.

Meet the bootstrap!

CakePHP's bootstrap.php will be loaded before the Router being called. In our case, we can check the URL, save it to registry using Configure::write and use it in our routes.php.

Put these codes in your /config/bootstrap.php

// get the URL, and split it using . so we can get the sub-domain
$url = explode('.', $_SERVER['HTTP_HOST'], 3); //creates the various parts
 
// if the array is 2, means they access using http://example.com and
// if we can check www in the third part, we use our pages controller
// to load our mainpage
if (count($url) === 2 OR $url[0] === 'www' ) {
 
    // default routing for main site
    Configure::write('Route.default', array('controller' => 'pages', 'action' => 'display'));
 
}
 
// okay, user use sub-domain and we have it, set the default routing
// and set the sub-domain name to our registry so we can use it later
else {
 
    // default routing for client's site
    Configure::write('Route.default', array('controller' => 'blogs', 'action' => 'display'));
 
    // set the subdomain to registry so we can use it later
    Configure::write('Shop.url', $url[0]);
}

Set the routing

Now, open your /config/routes.php and find the code below

Router::connect('/', array('controller' => 'pages', 'action' => 'display'));

This is the default routing where Cake will always load pages controller. We need to change this somehow, use the code below.

Router::connect('/', Configure::read('Route.default'));

Finish!

Done! Now you apps can be access using main domain or your user's sub-domain without having to create separate apps for it. No hacks, no .htaccess code, all pure CakePHP codes with some server configuration. Good luck!

Comments (5)

Leave a comment

You are commenting as guest.