How to handle multiple domains with CakePHP

Posted May 22nd, 2011 • 4 min read

Lately, we've been working with multiple environments/servers for our websites to be able to have them approved by clients before going live. However, following set up can also work nicely when you develop your sites locally and don't want to keep changing the configuration every time you upload it.

Setting up the database config file

So, what changes in your config? Not all that much. Let's have a look at the default database.php config file

class DATABASE_CONFIG {

    var $default = array(
    'driver' => 'mysql',
    'persistent' => false,
    'host' => 'localhost',
    'login' => 'user',
    'password' => 'password',
    'database' => 'database_name',
    'prefix' => '',
    );

    var $test = array(
    'driver' => 'mysql',
    'persistent' => false,
    'host' => 'localhost',
    'login' => 'user',
    'password' => 'password',
    'database' => 'test_database_name',
    'prefix' => '',
    );
}

The $default database config is used when you don't specify anything. The $test database is used by SimpleTest. Let's say you want to have a local environment and one for when you're on your live/production server. Below is the database config I use

class DATABASE_CONFIG {

    var $local = array(
        'driver' => 'mysql',
        'persistent' => false,
        'encoding' => 'utf8',
        'host' => 'localhost',
        'login' => 'root',
        'password' => 'root',
        'database' => '',
        'prefix' => '',
        'port' => '/Applications/MAMP/tmp/mysql/mysql.sock',
    );

    var $production = array(
        'driver' => 'mysql',
        'persistent' => false,
        'encoding' => 'utf8',
        'host' => 'localhost',
        'login' => '',
        'password' => '',
        'database' => '',
        'prefix' => '',
        'port' => '',
    );

    public function __construct() {
      if(isset($_SERVER['SERVER_NAME'])) {
        switch($_SERVER['SERVER_NAME']) {
          // Are we working locally?
          case 'www.YOURLOCALURL.com':
            $this->default = $this->local;
            Configure::write('debug', 2);
            break;
          case 'www.YOURLIVEURL.com':
            $this->default = $this->production;
            Configure::write('debug', 0);
            break;
          default:
            $this->default = $this->production;
            Configure::write('debug', 0);
            break;
        }
      } else {
        // If there's no SERVER_NAME we're probably using bake from the command line, so use local
        $this->default = $this->local;
      }
  }

}

As you can see we check the $_SERVER['SERVER_NAME'] . Locally I like to work with dev.domain.com domains, but if you're using some sort of localhost structure this will work fine as well. So, depending on the server(name) you're on the correct database credentials are put into the $default config. Obviously, you can extend the database configs as much as you want. We usually work with 4 arrays: local, development, staging and production. As a bonus, we can set the debug value for these environments accordingly as well.

Thoughts?

I have been considering putting this logic in the bootstrap.php, but not sure if this is where one would want this logic. Do you guys use a similar setup, or know ways to improve or extend this? Let me know in the comments!

Stay up to date

Want to know when a new post comes out and stay in the loop on tips, tricks and gotchas? Consider signing up for the Mindthecode newsletter.

Comments

Keep reading

September 18th, 2017 • 2 min read
We usually focus on getting the homepage Pagespeed to 90+, but what about the rest of the pages? I made a tool to help with this.
October 26th, 2012 • 2 min read
My experiences while converting this blog to an Octopress one
May 5th, 2014 • 11 min read
Today I want to show a generic workflow and setup I have used a lot lately when working on building apps with Angular. It uses Gulp as a CI system and Browserify to minimize code clutter and maximize awesomeness.