Yii 1.1: Making friends' configs, DB credentials and paths with version control

5 followers

In many cases developers of one project use their own local environments for coding. There may be a common test platform and, of course, production. All these environments usually have different DB credentials and directory structures (first of all, path to YII framework directory). Another feature is debugging. It's convenient to turn it on for local environments, eventually - on the test server and never (generally speaking) - on the production.

Moreover, it's a good option to have a possibility to export stable revision from SVN/Git to website's directory on a test or a production sever. But correct versioning suffers from different configs and index.php's.

In our company we use the following simple trick to avoid differences and always keep universal correct code in the repository.

Universal directory structure:

- _local
- protected
- www

The [_local] directory must be added to the ignored. Right it must be kept individually, depending on platform's credentials and paths.

The enter point [/www/index.php]:

<?php
mb_internal_encoding('UTF-8');
require('../_local/www.php');
require_once($yii);
Yii::createWebApplication($config)->run();

Local [/_local/www.php]:

define('YII_DIR', 'yii');
define('YII_PROTECTED', 'protected');
 
$yii=dirname(__FILE__).'/../../../'.YII_DIR.'/framework/yii.php';
$config=dirname(__FILE__).'/../'.YII_PROTECTED.'/config/main.local.php';
 
defined('YII_DEBUG') or define('YII_DEBUG',true);

Production [/_local/www.php] (example):

define('YII_DIR', 'yii-1.1.9.r3527');
define('YII_PROTECTED', 'protected');
 
$yii='/var/www/'.YII_DIR.'/framework/yii.php';
$config=dirname(__FILE__).'/../'.YII_PROTECTED.'/config/main.production.php';
defined('YII_DEBUG') or define('YII_DEBUG',false);

The same trick you may use for DB credentials.

[/protected/config/main.production.php]:

return array(
...
    'components'=>array(
            ...
            'db'=>require(dirname(__FILE__) . '/../../_local/db.php'),
            ...

[/_local/db.php]:

return array(
    'connectionString' => 'pgsql:host=10.0.0.17;port=5432;dbname=mydb',
    'emulatePrepare' => false,
    'username' => 'prod_user',
    'password' => 'prod_pw',
    'charset' => 'utf8',
    'schemaCachingDuration' => 3600,
);

You can inherit a general config file (versioned) and add/overload only certain params in unversioned (ignored) specific configs, using technincs well-described in articles this Wiki section.

PS: Prepared in common with snov.

Total 1 comment

#10282 report it
marcovtwout at 2012/10/17 08:29am
Yii extension

This extension will probably help anyone that wants to do a similar thing ;) http://www.yiiframework.com/extension/yii-environment/

Leave a comment

Please to leave your comment.

Write new article