tags:

views:

129

answers:

3

I have a local, staging and production environment for my CodeIgniter based site. Increasingly I find everytime I deploy a version I have more and more little bits of code to change because of server variations.

What would be a good (and quick) solution I could add that would allow me to set these variables by just using one setting. Where would be the best place to insert this in the index.php, some sort of hook?

A: 

The official CodeIgniter doc suggests this for multiple database environment:

Gobal variable in config.php file to set the environment:

$active_group = "test";

Multiple settings in database.php

$db['test']['hostname'] = "localhost";
$db['test']['username'] = "root";
$db['test']['password'] = "";
$db['test']['database'] = "database_name";
...

$db['production']['hostname'] = "example.com";
$db['production']['username'] = "root";
$db['production']['password'] = "";
$db['production']['database'] = "database_name";
...

See the doc for further details.

Yada
Yeah I have this but also many other variables in other files API keys etc. What would be the best way to manage these?
Alex
+2  A: 

define a "LIVE" constant which is TRUE or FALSE based on the current domain its on (put this in your index.php file)

if(strpos($_SERVER['HTTP_HOST'], 'mylivesite.com'))
{
    define('LIVE', TRUE);
}
else
{
    define('LIVE', FALSE);
}

and then check to see if you are live or not and assign the variables accordingly

if(LIVE)
{
    $active_group = "production";
}
else
{
    $active_group = "test";
}

ive been doing this with our 5 environment setup for the past year with no problems

Tom Schlick
A: 

Having many little bits of code that change because of server variations is a bad sign that you're modifying code that's not supposed to be modified for the application. The only things that you're supposed to change between servers is your configuration variables located in config.php, which should have default values for the developers to change depending on the environment.

Randell