Using Configure in CakePHP applications

I love CakePHP’s Configure class, available both in CakePHP 1.1 and 1.2 branches. It is extremely useful to store application settings that you may need application-wide. What I normally do is add this line to app/config/bootstrap.php:


And then I create a file named app/config/config.php with all my application specific settings, like so:

$config['Settings'] = array(
	'version' => '1.0.213',
	'title' => 'My Application'

You can naturally later access any of these settings by doing something like:

$title = Configure::read('Settings.title');

Now, I also like to categorize my settings in different sections, so my app/config/config.php may look a little more like this:

$config['Settings'] = array(
	'version' => '1.0.213',
	'title' => 'My Application'

$config['Cache'] = array(
	'queries' => true,
	'views' => false

But here comes the important tip: allways consider that someone else has already defined a setting with the same category you are specifying. Why do I say this? Because CakePHP 1.2 is moving all its settings to a Configure approach, and its already utilizing some configuration categories that you may be using, like App. In fact, if you just define a new App category, it will overwrite CakePHP’s built in App category and result in your application going to hell. Well, not hell particularly, but it will surely not work as expected. So instead, define each category like this:

$config['Settings'] = Configure::read('Settings');

$config['Settings'] = Set::merge(ife(empty($config['Settings']), array(), $config['Settings']), array(
	'version' => '1.0.213',
	'title' => 'My Application'

This way we are making sure that if there are settings already defined in that category, we add our own to it instead of overwriting it entirely.

Related posts:

  1. ExpectsBehavior, model unbinding in CakePHP 1.2
  2. Limiting the associated models returned with CakePHP
  3. Something to consider when saving HABTM records in CakePHP

Leave a Comment

12 Comments to "Using Configure in CakePHP applications"

  1. Sep172007 at 1:33 am

    Stephen [Visitor] wrote:

    Great job with CAKEPHP

  2. Nov122007 at 9:07 am

    Jonny [Visitor] wrote:

    Thanks buddy; great write up.

  3. Mar262008 at 7:14 am

    Webmaster Mexico [Visitor] wrote:

    I try to implement this example, but I lost the data into my app_model. I need to do something else?

  4. Jul032008 at 6:04 pm

    mark [Visitor] wrote:

    what would be the big difference between doing it that way and just defining constants – which are applicating-wide as well

    (of course, if the content of the config constants are not supposed to be changed anymore)

  5. Jul042008 at 12:39 am

    mariano.iglesias [Member] wrote:

    @mark: your “of course” statement pretty much answered your own question. If you are looking for static constants, then defines would work just fine (in fact, it would provide a small performance improvement.)

    However, if you are looking for variables that you don’t want fixed (since they could be overriden at the application level by using Configure::write, or while loading another configuration file with Configure::load), then Configure is your best friend.

  6. Jun232009 at 10:58 pm

    Gediminas [Visitor] wrote:

    Great job. But also need how to set config values from app_controller and so on

  7. Oct012009 at 8:12 pm

    oes tsetnoc [Visitor] wrote:

    Does anyone know what I have to change for showing the thumbnail and just the excerpt with it??

  8. Jan022010 at 10:25 am

    adnan wrote:

    Configure::write(‘Settings’, array(
    ‘version’ => ’1.0.213′,
    ‘title’ => ‘My Application’

    does not save the value to config file.. How can I update the config file using Configure::write ??

  9. Jan022010 at 12:53 pm

    mariano wrote:

    Of course that wouldn’t save to the file. The Configure class is not about writing to the configuration file you parse from, but just to parse it. You’d have to extend the Configure class yourself, and make it so after a write it saves to the file.

  10. Jan292010 at 9:26 pm

    Tad wrote:

    @adnan: Use Configure::store(). It’s in the API docs.

  11. Feb112010 at 10:06 am

    Tom wrote:

    Thanks for the quick reference, I’m always forgetting the syntax of this stuff ;) I am also a coder with a very troubled mind this morning lol!

  12. Mar232010 at 11:32 am

    Hans Heidemann wrote:

    Thank you for this very helpful introduction into using configure. Since I do a lot of programming with CakePHP I was looking for a way to implement a configuration file without having trouble when updating CakePHP.

Powered by Wordpress and MySQL. Clauz's design for by Cricava