Welcome Guest, Not a member yet? Register   Sign In
Supporting multiple configurations for production and development
#1

[eluser]Unknown[/eluser]
Hello,

In the past (developing outside CI) I defined configuration settings based upon the value of $_SERVER['HOSTNAME']. Using this methodology, I could have production config settings and development config settings that would automagically get set depending on what environment the application was running on.

In CI, what is the best practice for achieving this same effect?

Thank You.
#2

[eluser]bretticus[/eluser]
The easiest way is to just maintain a different system/application/config/config.php file on each host. If you are using a database, you can also keep a separate system/application/config/database.php file in addition.

I run into problems when using subversion, so I typically do not add these files to my repository. Saying that, it would be easier to have it auto detect a configuration, but it's not too difficult for me to manage so I haven't gone that route.

One thing you might try however, is to extend the config library. You can replace it entirely or extend it. See Creating Core Classes. The constructor of the core config library calls get_config from Common.php

Code:
//constructor
function CI_Config()
{
    $this->config =& get_config();
    log_message('debug', "Config Class Initialized");
}

Code:
function &get;_config()
{
    static $main_conf;

    if ( ! isset($main_conf))
    {
        if ( ! file_exists(APPPATH.'config/config'.EXT))
        {
            exit('The configuration file config'.EXT.' does not exist.');
        }

        require(APPPATH.'config/config'.EXT);

        if ( ! isset($config) OR ! is_array($config))
        {
            exit('Your config file does not appear to be formatted correctly.');
        }

        $main_conf[0] =& $config;
    }
    return $main_conf[0];
}

You could simply extend the Config class to use a different constructor with logic to pick a different config file based on $_SERVER[‘HOSTNAME’].

Also, be sure to check the forum, this question has been asked and answered many times it seems.

Good luck!
#3

[eluser]n0xie[/eluser]
[quote author="bretticus" date="1250802744"]
I run into problems when using subversion, so I typically do not add these files to my repository. Saying that, it would be easier to have it auto detect a configuration, but it's not too difficult for me to manage so I haven't gone that route.
[/quote]
We usually add a config.default.php and a database.default.php to the repository and disallow both config.php and database.php. This way each host/server is forced to have his own config file, which is much easier to maintain.
#4

[eluser]bretticus[/eluser]
[quote author="n0xie" date="1250803140"]
We usually add a config.default.php and a database.default.php to the repository and disallow both config.php and database.php. This way each host/server is forced to have his own config file, which is much easier to maintain.[/quote]

Thanks n0xie, that is a great idea. Simple and elegant.
#5

[eluser]John_Betong[/eluser]
 
>>> The easiest way is to just maintain a different system/application/config/config.php file on each host.
>>> If you are using a database, you can also keep a separate system/application/config/database.php file in addition.
 
I prefer having one set of code that works on both my Localhost and live sites.
 
In my standard setup, every call to CodeIgniter passes through index.php so I test and define this "LOCALHOST" variable, then use it to configure the setup variables:
Code:
// index.php
     define('LOCALHOST', 'localhost' === $_SERVER['SERVER_NAME']);
  
     ini_set('display_errors', LOCALHOST ? 'On' : 'Off');
     $system_folder = LOCALHOST ? "ci_svn/system" : "ci_system";


   // config.php
   $config['log_threshold'] = LOCALHOST ? 4 : 0; // either 0 or 1

   // databse.php
   $db['default']['database'] = LOCALHOST ? "iching" : 'jjokes_iching' ;
 
 
 edit: spelling and added LOCALHOST
 
#6

[eluser]bretticus[/eluser]
That's awesome John_Betong.

I never followed the code all the way through. I assumed stuff like this gets overridden in Codeigniter.php.

UPDATE:

Oh whoops! I get it now. It's just like you said, you change the system folder dynamically by host header. Very cool.
#7

[eluser]John_Betong[/eluser]
[quote author="bretticus" date="1250844455"]That's awesome John_Betong.

I never followed the code all the way through. I assumed stuff like this gets overridden in Codeigniter.php.

UPDATE:

Oh whoops! I get it now. It's just like you said, you change the system folder dynamically by host header. Very cool.[/quote]
 
Many thanks, I don't know where I got the idea but I adapted it to so set once then forget.
 
The quote that springs to mind is "standing on the shoulders of giants".
 
 
 
#8

[eluser]Unknown[/eluser]
Thanks everyone!! Great ideas! I'm going to try John_Betong's idea and see how that works out.




Theme © iAndrew 2016 - Forum software by © MyBB