Dessert #5 - Keep a custom configuration file

Posted by Felix Geisendörfer, on Sep 16, 2006 - in PHP & CakePHP » Core & Hacking

Deprecated post

The authors of this post have marked it as deprecated. This means the information displayed is most likely outdated, inaccurate, boring or a combination of all three.

Policy: We never delete deprecated posts, but they are not listed in our categories or show up in the search anymore.

Comments: You can continue to leave comments on this post, but please consult Google or our search first if you want to get an answer ; ).

One thing that I found pretty useful when developing CakePHP applications, is to put an additional configuration file with definitions and settings specific to the current project into the /app/config folder. So for example when working on an application called lugsteinhof, I would put this into my bootstrap.php:

php
  1. require_once(APP.'config'.DS.'lugsteinhof.php')

Then I would create a file in /app/config/ called lugsteinhof.php and fill it with information like this:

php
  1. setlocale(LC_ALL, 'de_DE@euro', 'de_DE', 'deu_deu');
  2.  
  3. /**
  4.  * Figure out whether the script is running on the local development machine, or
  5.  * in an production enviornment. You can also manually define this value above
  6.  * in which case the code below won't get executed
  7.  */
  8. if (!defined('SERVER_ENVIRONMENT'))
  9. {
  10.     if (strpos(env('DOCUMENT_ROOT'), 'C:/Localhost')!==false)
  11.         define('SERVER_ENVIRONMENT', 'development');
  12.     else
  13.         define('SERVER_ENVIRONMENT', 'production');
  14. }
  15.  
  16. /**
  17.  * The default theme if none is available via database
  18.  */
  19. define('DEFAULT_THEME', 'summer');
  20.  
  21. // More definitions and settings ...

Oh and if you are curious about the SERVER_ENVIORNMENT definition, I use it to automatically switch between development & production database configuration. To do this I simply modified my /app/config/database.php like this:

php
  1. class DATABASE_CONFIG
  2. {
  3.     var $default = null;
  4.    
  5.     var $development = array('driver'    => 'mysql',
  6.                              'connect'  => 'mysql_connect',
  7.                              'host'     => 'localhost',
  8.                              'login'    => 'production-user',
  9.                              'password' => 'production-pw',
  10.                              'database' => 'lugsteinhof',
  11.                              'prefix'    => '');
  12.  
  13.     var $production    =  array('driver'    => 'mysql',
  14.                                 'connect'  => 'mysql_connect',
  15.                                 'host'     => 'localhost',
  16.                                 'login'    => 'dev-user',
  17.                                 'password' => 'dev-pw',
  18.                                 'database' => 'dev-lugsteinhof',
  19.                                 'prefix'    => '');
  20.  
  21.     function DATABASE_CONFIG()
  22.     {
  23.         $this->default = $this->{SERVER_ENVIRONMENT};
  24.     }
  25. }

Btw., I took the idea of automatically checking whether the code is running on my dev machine or in production enviornment from Jonathan Snook's post called Ease Deployment with Automatic Server Check.

--Felix Geisendörfer aka the_undefined