Custom configuration files in CakePHP

Written by: daniel

Date 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);
}

?>

2 Responses to “Custom configuration files in CakePHP”

  1. Krzysztof said:

    Great thank. That I was looking for.
    Cheers.

  2. Toby G said:

    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)

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>