Wednesday, February 6, 2013
0 comments

Using Configure in CakePHP applications

5:47 PM
Using Configure in CakePHP applications | Coding My Thoughts <!--[if IE 6]><![endif] 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:
Configure::load('config');
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'
));

0 comments:

 
Toggle Footer