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

Print this Post | Digg This | Stumble It | Delicious

6 Comments

[...] Andy Dawson (aka AD7six) and Felix Geisendörfer (aka the_undefined) presented in the last 24 hours solutions for switching the database configuration based on some criteria. These solutions work fine, but personally I use a different approach. [...]

Nate K on Sep 16, 2006:

Very good tips! I actually have a few more items in a custom config file, depending on the project and its needs. Having a switch for development/production is such a timesaver - no matter what the size of the project. You can do many other things with this as well.

jacmgr on Jan 26, 2007:

I am curious for exampleof how you set theme using:
define('DEFAULT_THEME', 'summer');

Thankx!
John

Felix Geisendörfer on Jan 27, 2007:

jacmgr: You mean you want to see an example for theming in CakePHP? Yeah, I think I should write about this, that's a good idea ; ).

[...] Keep a custom configuration file [...]

eregilkibly on Dec 06, 2007:

Jungs, habt ihr eigentlich Angst vor spontaner Erektion? Text: penni-dreyer. Seite 1. Immer zum Wochenende. Jungs fragen Madchen fragen Jungs. vardenafiltadalafil bestellen

Add a comment