CodeIgniter Forums

Full Version: Hostname specific configurations
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

Any one have a good idea of how to use different configuration settings based on the actual hostname and/or basedirectory?

When I run on localhost I have one baseurl, one set of userid/passwords/database, when testing on a live server these changes and the same when I go to full production.

Currently I have an if statement in the database configuration file that loads different settings based on hostname, but I'd rather not repeat that too much if there is a smarter way Smile

El Forum

could your IF statement live in config > config.php and store a value in a config var, then be referenced by other files using $this->config->item('item_name') ?

El Forum

Hmmm I am not sure I understand exactly what you mean?

For the database settings I have 3 different servers the code needs to run on - so I need to set databasename,userid,password, host etc just for that. The same goes for baseurl and possibly others I haven't found out about yet Wink

El Forum

But you just need to overwrite a handful of variables, right? For three different hosts - so perhaps a dozen lines of code will cover it.

How about the variant described in [url="/wiki/Automatic_base_url/"]the wiki[/url] and that article's corresponding [url="/forums/viewthread/81424/"]thread in the forums[/url]. Seems easy enough to adapt that approach to include DB config settings.

El Forum

The base url solution would work great for that particular setting and the switch statement looks neater than my if's but gives the same result. I will try and see how many different settings need to have - if there is too many I might change the loading of the actual config file and database file to load them based on hostname and add the hostname to the filename.

Thank you for your input Smile

El Forum

In my own development I have the same filestructure and same db settings for local, testing and production server and I dont have to touch settings at all. Theres actually no need to use full url in the base_url setting and you can set it to(for example):
$config['base_url'] = '/';
Browser will understand in which host you are on Wink

For the database part, if you insist using different usernames, pw's or databasenames, you just make different connection sets for each as told in the user guide. Then on each server you just replace the $active_group depending on what server it is. You only have to save the config file once to each server so I dont think its a big deal.

El Forum

I was looking for a more general solution that could handle it without the chance of overriding production settings with test settings etc..

The reason the various databases are with different userids/passwords are because its on different web hosts where we cannot change the password nor userids.

It is not that big a problem and I can manage it with if or switch statements - I was just wondering if a solution that was more clean existed already.

But once again - thank you for your help and time Big Grin

El Forum

[eluser]Phil Sturgeon[/eluser]
How to: Support multiple production environments in CodeIgniter :coolsmile:

El Forum

Hehe - I already went there and read it - It's a nice solution, but I will still end up with quite a few if statements in each configuration file.

I am thinking of adding the configuration directory to the /index.php so I could end up with

$configuration_folder = "config/".$_SERVER['SERVER_NAME'];

That way I could have config/productionurl, config/testurl, config/localurl etc... and copy each set of files to the specific folders..

I have also looked at changing Common.php - &get;_config() to have a new variable like
$config_file = "config_".$_SERVER['SERVER_NAME'];
and then use if ( ! file_exists(APPPATH."config/$config_file".EXT))

That would be the preferred way as it would allow me to do config files the way I am used to.. Smile Only problem with the two possible solutions is that they change the core functionality of CI... which I am not quite happy to do...

El Forum

In a similar situation I created templates for all configuration files that have unique settings for each environment. Ex: config.php.tpl, database.php.tpl . On each server, i copy them without the tpl extension and set the appropriate values. This works with svn too, config.php.tpl will be versioned but config.php will not be. Send out a note to all developers to not include the config.php while committing the code.