You are viewing revision #1 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.
This method was inspirated on the excellent text about different environments available at http://www.yiiframework.com/doc/cookbook/32/
SCENARIO: *** Suppose you have a project with an increasing number of modules or configurations. So, not rarely you have to change your config/main.php file.
This is troublesome when you work on an environment with development/test/production scenarios. As a simple developer, you must NOT have access to production DB passwords and, consequently, you must not have access to config/main.php at production (it contains the db password).
You will annoy your DBAs every time you must to post a change at config/main.php, because you'll have to ask them to change the password from dev to production password. This leads to very potencial problems:
- You can forget this and break your system (and every module within it);
- To have not a DBA available and your system update will have to wait.
SOLUTION: *** To overcome this problem, I suggest to make use of CMap::mergeArray function to encapsulate db and module configurations in specific files. Making this, you can have these properties protected from careless using (you can still apply some other security actions on them).
Let's see how to do this:
Create a db.php file at config directory. Then, specify your db configurations:
db.php
return array(
'components'=>array(
'db'=>array(
'connectionString'=>'my_connection_string',
'username'=>'my_user',
'password'=>'my_password',
),
),
);
OPTIONALLY, you can create a modules.php file to encapsulate modules properties. Create it at 'config' dir too.
modules.php
return array(
'modules' => array(
'module1' => array(
),
'module2' => array(
),
...
)
);
Now, the key to this method.
Once you have the pre-config files above, you can change config/main.php as below:
config/main.php (at the very beginning):
$pre_config =
CMap::mergeArray(
require(dirname(__FILE__).'/modules.php'),
require(dirname(__FILE__).'/db.php')
);
This will load db and modules configurations to the variable $pre_config
So, we will merge this configurations with the others at main.php (after the last code block):
return CMap::mergeArray($pre_config, array(
'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
'name'=>'My Project',
...
)
);
Remember to NOT redefine db or module configurations at main.php, instead you will overwrite the previous configurations.
Now you have three configuration files: main.php, db.php and modules.php (I thought about to create a components.php too, but it sounded as unnecessary)
*ps: English corrections are welcome. I am not an expert =)
:)
Thank you Jerry, your method worked great...
example /protected/config/db.php:
return array( 'connectionString'=>'my_connection_string', 'username'=>'my_user', 'password'=>'my_password', 'charset'=>'utf8' );
in /protected/config/main.php:
... 'db'=>require(dirname(__FILE__).'/db.php'), ...
how about this method?
'db'=>require(dirname(FILE).'/db.php'),
'modules'=>require(dirname(FILE).'/modules.php'),
svn application
This method is very confortable if you are managing update with svn (or any other revision control system).
The database configuration is a part of config file that cannot reside under svn control, because each developer has his own dbsystem and the production copy too has his own config.
Using this method we can create a small, separed config file for db only, that we will 'ignore' in svn, that allows to keep the main configuration file under revision control and at a time avoid to override the db configuration.
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.