The extension is designed for flexible application configuration. Represents the behavior for the СApplication class. Adds an object with one method - loadConfigure().
Features ¶
- Easy connection configuration file;
- Inheritance of configuration files;
- Automatic load config from *_local.php;
Settings ¶
- string FConfig::configDir - path to the configuration file, defaults to 'application.config';
- array FConfig::configs - array with a list of configurations, key - the name of the configuration, value - an array of configuration settings;
- string FConfig::currentConfig - the name of the current configuration;
Each configuration has a property 'parent' - is a string with the name of the parent configuration.
Example 1 ¶
I need to have two different application configuration mode for the development and production server. Most of the settings they have the same, and some do not. To do this, I have three files in the folder config: main.php - contains general settings for all configurations; dev.php - contains specific design-time configuration; production.php - contains settings specific to the production.
index.php file I have contains the following code:
<?php
$yii = 'yii/framework/yii.php';
// remove the following lines when in production mode
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);
require_once($yii);
Yii::createWebApplication(array(
'behaviors' => array(
'fconfig' => array(
'class' => 'ext.FlexibleConfig.FConfig',
'currentConfig' => 'dev',
'configs' => array(
'dev' => array(
'parent' => 'main',
),
'production' => array(
'parent' => 'main',
),
),
),
),
))->loadConfigure()->run();
In addition to the config folder is a file dev_local.php, which is automatically loaded after dev.php and overrides the necessary local settings.
Example 2 ¶
index.php
<?php
$yii='yii/framework/yii.php';
$config=include(dirname(__FILE__).'/protected/config/main.php');
require_once($yii);
Yii::createWebApplication($config)->loadConfigure()->run();
config/main.php
<?php
if (strpos($_SERVER['HTTP_HOST'], 'localhost') !== false) {
defined('YII_DEBUG') or define('YII_DEBUG',true);
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);
define('YII_ENV', 'dev');
} else {
defined('YII_DEBUG') or define('YII_DEBUG',false);
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',1);
define('YII_ENV', 'production');
}
return array(
'name'=>'My Web Application',
'behaviors' => array(
'fconfig' => array(
'class' => 'ext.FlexibleConfig.FConfig',
'currentConfig' => YII_ENV,
'configs' => array(
'dev' => array(),
'production' => array(),
'test' => array(),
),
),
),
'params'=>array(
'adminEmail'=>'ekaragodin@gmail.com',
),
);
Resources ¶
Change Log ¶
January 26, 2011 ¶
- Added multiple inheritance
January 23, 2011 ¶
- First version
further clarification needed
I don't understand
Could you please elaborate?
autoload *_local.php
If the current configuration of the "dev", then after loading the file "dev.php" automatically loaded "dev_local.php", if it exists.
The result is CMap::mergeArray(require('dev.php'), require('dev_local.php'));
looks usefull
thanks for sharing!
Definately be using this.
This has been the one thing that we've been hacking about with a lot but never having the time to write a proper solution. I'm assuming you can have parents with parents?
What we have been doing though is making use of an environment variables APPLICATION_ENV to control which config file we load. We followed the Zend structure for this so we could deploy projects in either framework, so it's either development, testing or production. Of course this can be passed into current config and you're covered if you need to overwrite the index file.
dynamic define
Certainly, inheritance can be as much as long.
Not to change a file index.php, it is possible a array transferred in Yii::createWebApplication() too to take out in an external file or to define a current config dynamically:
'currentConfig' => strpos($_SERVER['HTTP_HOST'], 'localhost') !== false ? 'dev' : 'production',
A better config system is needed in Yii...
Thanks to the author, it's a step in the good direction. A better config system is really needed in Yii, but IMHO this extension is still far from it.
Here are several cons:
Having to modify "index.php" in almost each new instance of a standard Yii applications is a real PITA.
example 2
I added a second example to show the use case without changing the index.php file.
easy to complain about something ...
instead, do your part and help the extension get better
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.