Skip to content

Instantly share code, notes, and snippets.

@janetriley
Last active August 29, 2015 14:08
Show Gist options
  • Save janetriley/28e3656d2d54d78d3aab to your computer and use it in GitHub Desktop.
Save janetriley/28e3656d2d54d78d3aab to your computer and use it in GitHub Desktop.
A way to override Yii configuration settings with local settings.
<?php
//An excerpt from Yii's index.php file
$yii_path=dirname(__FILE__).'/yii/yii.php';
//The default set of config settings
$base_config = require(dirname(__FILE__).'/protected/config/main.php');
// Optionally redefine local settings in $local_config_path. These include:
// * your $yii_path path, relative to index.php
// * define YII_DEBUG,
// * your local database settings
//
// $local_config_path will execute any code as well, so you can define trace level
$local_overrides = array();
//An optional
$local_config_path = dirname(__FILE__).'/protected/config/local-main.php';
if( file_exists($local_config_path)){
$local_overrides = include($local_config_path);
}
//This method isn't available yet in our version of Yii - got to wait for 1.1.16.
//Cut and pasted from
//https://github.com/yiisoft/yii/blob/1.1.16-branch/framework/collections/CMap.php
/**
* Merges two or more arrays into one recursively.
* If each array has an element with the same string key value, the latter
* will overwrite the former (different from array_merge_recursive).
* Recursive merging will be conducted if both arrays have an element of array
* type and are having the same key.
* For integer-keyed elements, the elements from the latter array will
* be appended to the former array.
* @param array $a array to be merged to
* @param array $b array to be merged from. You can specify additional
* arrays via third argument, fourth argument etc.
* @return array the merged array (the original arrays are not changed.)
* @see mergeWith
*/
function mergeArray($a,$b)
{
$args=func_get_args();
$res=array_shift($args);
while(!empty($args))
{
$next=array_shift($args);
foreach($next as $k => $v)
{
if(is_integer($k))
isset($res[$k]) ? $res[]=$v : $res[$k]=$v;
elseif(is_array($v) && isset($res[$k]) && is_array($res[$k]))
$res[$k]= mergeArray($res[$k],$v);
else
$res[$k]=$v;
}
}
return $res;
}
//Overwrite the base values with whatever overrides were set
$config = mergeArray( $base_config, $local_overrides);
// if not already specified in local-main,
// specify how many levels of call stack should be shown in each log message
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);
//Start the application as usual
require_once($yii_path);
include('global.php');
Yii::createWebApplication($config)->run();
<?php
//protected/config/local-main-template.php can be checked into version cotrol
//Copy this to protected/config/local-main.php
//Ensure that .gitignore includes local-main.php your settings aren't pushed to other environments.
// LOCAL Yii Path
//Define the Yii directory path on your machine, relative to this config file
$yii_path=dirname(__FILE__).'/../../YOUR_DIR/yii.php';
// Crank up the debug!
defined('YII_DEBUG') or define('YII_DEBUG',true);
// specify how many levels of call stack should be shown in each log message
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);
// Redefine any of the properties in main.php here
// They'll be merged in index.php
return array(
'name'=>'My Local Yii Server',
// application components
'components'=>array(
'db'=>array(
'connectionString' => 'mysql:host=YOUR_DB_HOST;dbname=YOUR_DB_NAME',
'username' => 'YOUR_DB_LOGIN',
'password' => 'YOUR_DB_PASSWORD',
),
),
// application-level parameters that can be accessed
// using Yii::app()->params['paramName']
'params'=>array(
// this is used in contact page
'adminEmail'=>'YOUR@EMAIL.HERE',
),
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment