Welcome Guest, Not a member yet? Register   Sign In
Codeigniter how to conditionally load config files?
#1

[eluser]sabya[/eluser]
I am using CodeIgniter to write a application. I need to create something like this: - I will have: -

* my_config.php
* config_production.php
* config_development.php

Now, my_config.php will be autoloaded. From there, if it is production server config_production.php will be loaded. Else config_development.php will be loaded.

how should I do this?

I tried doing like this in my_config.php: -
Code:
<?php
if(gethostbyaddr ("127.0.0.1") == 'hello.sabya'){
    $this->config->load('config_production');
} else {
    $this->config->load('config_development');
}
?>

It is not working because, the "$this->config" is not initialized yet.

How can I achieve this?
#2

[eluser]Dam1an[/eluser]
Can you not just include the file with normal PHP?
Code:
include APPPATH.'config/config_development.php';
#3

[eluser]Phil Sturgeon[/eluser]
That would work as long as you made the include in a config file that WAS being included correctly. Otherwise the config items wont accessable via $this->config->item().

Take a look at How to Support multiple production environments in CodeIgniter which should help you with some of this.
#4

[eluser]Dam1an[/eluser]
Phil, I think your link is a bit screwed up, it's
Code:
http://philsturgeon.co.uk/news/2009/06/news/2009/01/How-to-Support-multiple-production-environments-in-CodeIgniter.html

Which seems wrong, not to mention the fact it just goes to the news page, instead of said article
I think all that publicity of being mentioned on PHP Developer has gone to your head Wink
#5

[eluser]Phil Sturgeon[/eluser]
That'll teach me to copy links directly from the address bar suggestions!
#6

[eluser]sabya[/eluser]
Thanks guys!

I got it working, like this: -
Code:
$ci = & get_instance();

if (gethostbyaddr ("127.0.0.1") == 'production.example.com') {
    //Load production configuration
    $ci->load->config('config_production');
} elseif(gethostbyaddr ("127.0.0.1") == 'staging.example.com') {
    //Load staging configuration
    $ci->load->config('config_staging');
} else {
    //Load development configuration
    $ci->load->config('config_development');
}




Theme © iAndrew 2016 - Forum software by © MyBB