Encapsulate db and module configurations

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 =)

7 0
9 followers
Viewed: 21 124 times
Version: 1.1
Category: Tutorials
Tags: module
Written by: ricardograna
Last updated by: Yang He
Created on: Oct 20, 2009
Last updated: 12 years ago
Update Article

Revisions

View all history

Related Articles