Simple Multiple Environment Setup with Default Yii Installation

After installing Yii basic application on our systems, most of the time we want to have multiple environment such as local, dev and live setup. Most of the times when we want to make changes to configuration file, we have to over-write configurations file for each environment.

We can make this process simple, while creating config file for each environment and adding a constant variable to index.php file to active a specific environment.

Following are the steps:

Step # 1

Add the following code to index.php

define(ENV, 'local');

// change the following paths if necessary
$yii=dirname(__FILE__).'/../yiiroot/framework/yii.php';

if (ENV == 'local') $config=dirname(__FILE__).'/protected/config/main-local.php';
if (ENV == 'dev')   $config=dirname(__FILE__).'/protected/config/main-dev.php';
if (ENV == 'live')  $config=dirname(__FILE__).'/protected/config/main.php';
Setp # 2

Create two files 'main-local.php' and 'main-dev.php' inside '/protected/config/' folder. These two files should be exact copy of main.php file.

Setp # 3

Make changes to each configuration file according to your requirements and then in 'index.php' set the constant variable to either

  • local
  • dev
  • live

as per environment setup.

As we know that there are many other advance approaches available, but this is a simple way to work with multiple environment.