Often times you want to be able to specify configuration parameters or settings that only apply to a single environment. These local configuration don’t need to, and shouldn’t be entered into version control, and should over ride default values. I needed a solution for a project I was working on so I wrote one for Yii.
The main configuration file protected/config/main.php returns an array of parameters. Edit this file to merge 2 arrays, 1 from main.php, and another from local.php.
Edit main.php to look like this:
<?php return CMap::mergeArray( array( 'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..', 'name'=>'Web app', ... other parameters ... 'params'=>array( // this is used in contact page 'adminEmail'=>'webmaster@example.com', ), ), local_config() ); // return an array of custom local configuration settings function local_config() { if (file_exists(dirname(__FILE__).'/local.php')) { return require_once(dirname(__FILE__).'/local.php'); } return array(); };
And then add any of your own configuration to local.php in the same config directory.
<?php return array( 'components'=>array( 'db'=>array( 'connectionString' => 'mysql:host=localhost;dbname=db_name', 'username' => 'my_user', 'password' => 'secret', 'enableParamLogging'=>true, ), 'log'=>array( 'class'=>'CLogRouter', 'routes'=>array( 'file'=>array( 'class'=>'CFileLogRoute', 'levels'=>'trace, info, error, warning', ), 'profile'=>array( 'class'=>'CProfileLogRoute', 'report'=>'summary', ), ), ), ), 'params'=>array( // this is used in contact page 'adminEmail'=>'yourself@example.com', ), );
Here we’ve overridden the adminEmail parameter to yourself@example.com, we’ve added in custom database username and password, and we’ve enabled logging.
Feel free to use this as a straightforward way to add custom config values to your Yii project. Just remember to make local.php an ignored file in Git or Subversion.