Yii 1.1: Simple Multiple Environment Setup with Default Yii Installation

7 followers

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.

Total 2 comments

#16210 report it
Volli at 2014/01/29 04:28am
simple

Hi there. That is really a useful but simple approach. If you work with automatic deployments, you don't want to edit a source file! So the ENV option should come from outside the code...

#16206 report it
mahdi1986 at 2014/01/27 11:43pm
good

thanks for wiki, this is a good and we can put config debug in dev or local,

if(ENV == 'dev' or ENV=='local' ) { defined('YII_DEBUG') or define('YII_DEBUG',true); defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3); }

Leave a comment

Please to leave your comment.

Write new article