RSS

Neil Crookes

Learnings and Teachings on Web Application Development & CakePHP

Nov

28

Runtime Config in CakePHP apps

Inspired by Chris Hartjes blog post titled Handling Multiple Environments In Your PHP Application, I decided to post what I do that’s similar in my CakePHP apps.

Share and Enjoy:

  • Digg
  • del.icio.us
  • StumbleUpon
  • Technorati
  • Slashdot

These are my runtime settings, options and defaults:

Key Options Default
site_or_admin site|admin site
environment live|local|staging live
operating_system linux|windows linux
invoked webserver|commandline webserver

The default settings are those that the code will most often run with and are set up in a configuration file:

APP/config/config.php

<?php
$config = array(
// ... other config settings
'Runtime' => array(
'site_or_admin' => 'site',
'environment' => 'live',
'operating_system' => 'linux',
'invoked' => 'webserver',
),
// ... other config settings
);
?>

These are then loaded into the CakePHP Configure class and the values are overwritten if required depending on various tests:

APP/config/bootstrap.php

<?php
Configure::load('config');
if (php_sapi_name() == 'cli') {
Configure::write('Runtime.invoked', 'commandline');
}
if (strstr($_SERVER['HTTP_HOST'], 'local')) {
Configure::write('Runtime.environment', 'local');
}
if (isset($_SERVER['windir']) || isset($_SERVER['WINDIR'])) {
Configure::write('Runtime.operating_system', 'windows');
} elseif ((
Configure::read('Runtime.invoked') == 'webserver'
&& strstr($_SERVER['HTTP_HOST'], 'staging')
) || (
Configure::read('Runtime.invoked') == 'commandline'
&& strstr($_SERVER['HOSTNAME'], 'dev-000')
)) {
Configure::write('Runtime.environment', 'staging');
}
?>

This isn’t great code I know, but it works for me and everyone at my work, apart from one of my colleagues who has a Mac (with a 30′ screen while I have to suffice with 2 x 19″s… bar steward), but you get the idea. It’s so useful to have these runtime settings set up and available globally so you don’t have to do the tests everytime you need to know something about your environment within your application code. Don’t copy it though ‘cus there’s obviously some values in here that’s specific to my staging environments etc, but you can copy the idea… if you want to?

Finally, there’s the code that determines whether the current request is in the ’site’ or the ‘admin’ section of your application. By ‘admin’ I mean admin routing. I do this in my AppController, but you can probably check straight from the Router class.

APP/app_controller.php

<?php
class AppController extends Controller {
var $layout = 'site';
function beforeFilter() {
$this->_checkAdmin();
}
function _checkAdmin() {
if (Configure::read('Routing.admin')
&& isset($this->params[Configure::read('Routing.admin')])
&& $this->params[Configure::read('Routing.admin')] = Configure::read('Routing.admin')) {
$this->layout = 'admin';
Configure::write('Runtime.site_or_admin', 'admin');
}
}
?>

Got any other suggestions for useful runtime settings? Or any improvements to the code? Add a comment.

Share and Enjoy:

  • Digg
  • del.icio.us
  • StumbleUpon
  • Technorati
  • Slashdot
(1 votes, average: 5.00 out of 5)
Loading ... Loading ...

8 Responses so far

Are the settings in the config file really necessary? I think it would be cleaner to define all those runtime settings (probably with the exception of “site_or_admin”) in one single place. For example:

// in bootstrap.php
class Runtime {
public static function getOS() {
// if OS is win, return “Windows”, else “Linux”
}
// here the other methods
}

Configure::write(‘Runtime.operating_system’, Runtime::getOS());

I hope you see what I mean ;-)

Hi Daniel,

Thanks for your comment.

I take your point about encapsulating it in one class. Good thinking.

I think I’ll change it to work that way.

I would make it cleaner and more portable.

Cheers.

[...] Runtime Config in CakePHP apps [...]

[...] alone in the corner while everyone else dances the night away, I suggest you read the posts by Neil Crookes and Chris [...]

Hello Neil,
Am facing a problem with this function
Configure::write(‘debug’, 0);
Whenever i install my cakephp in my Production server, i cannot access any of my screens. i keep getting an error. but if i change the value from 0 to say 3, & then back to 0, then i am able to view all ,y screens. i dont wanna keep doing this all the time. so can u tell me a solution for this.
thank u :)

Did you change your DB schema? Going from 0 to 1 or 2 refreshes the app’s cache files, alternatively delete the files in tmp/cache

Thank u for ur reply Neil. i did change my DB schema. i have also deleted files from tmp / cache folder. still nothing seems to work.
any other way to do this ?
thanks in advance..

@Naveen, checking your server error logs is the only other thing I can suggest.

Leave a comment