Difference between #3 and #2 of Encapsulate db and module configurations

Encapsulate db and module configurations
This method was inspirated on the excellent text about different environments
available at

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.

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:

return array(

OPTIONALLY, you can create a modules.php file to encapsulate modules properties.
Create it at 'config' dir too.

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

<b>config/main.php</b> (at the very beginning):
$pre_config = 

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(
	'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: <b>main.php</b>,
<b>db.php</b> and <b>modules.php</b> (I thought about to
create a components.php too, but it sounded as unnecessary)

*ps: English corrections are welcome. I am not an expert =)

### Links
Write new article
  • Written by: ricardograna
  • Updated by: Yang He
  • Category: Tutorials
  • Yii Version: 1.1
  • Votes: +7 / -1
  • Viewed: 13,117 times
  • Created on: Oct 20, 2009
  • Last updated: Jun 30, 2012
  • Tags: module