Custom configuration files in CakePHP
Written by: daniel
September 19, 2008
In order to speed up our development process and following the DRY rule we always try to split our code in reusable modules. Then when creating a new project we just get them one by one and add to the current code. Its pretty easy and efficient (now we can spend this time on adding new cool features) but there is one problem when this custom modules need some extra functions or configuration.
Let’s take a look for example at our contact module, that creates a simple contact form. In order to work properly, it needs to have these variables configured when cake starts:
Configure::write(’Admin.name’, ‘John Smith’);
Configure::write(’Admin.email’, ‘admin@example.com’);
This way, we can use this data across our application and they can be changed easily if necessary.
OK, as we know this can be added easily to the bootstrap.php file, but there is always a pain to remember to do it when installing a module. And if you have a dozen of modules this becomes a big hassle to go and manually add stuff for each of them.
Our solution is to create a custom directory in app/config, let’s say its my_config, and then command cake to load at bootstrap all files it finds there. Now our modules can provide their own configuration files which are included automatically.
The function that loads these files is placed in /app/config/bootstrap.php and it’s pretty simple:
<?php
//include all php files from app/config/my_config
foreach(glob(APP.”config/my_config/*.php”) as $configFile) {
include($configFile);
}
?>
Posted in



April 8th, 2010 at 2:24 pm
Great thank. That I was looking for.
Cheers.
May 25th, 2011 at 11:17 am
Just what I was looking for, as have been putting all my config in the bootstrap, but felt it was just a little too messy! :o)