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
(2 votes, average: 4.00 out of 5)
Loading ... Loading ...

Comments are closed.