Reputation: 11884
I need to create something similar to the following within my CodeIgniter project:
my_config.php
config_production.php
config_development.php
Now, my_config.php
will be autoloaded. From there, if it is a production server, config_production.php
will be loaded; else config_development.php
will be loaded.
How should I go about executing this?
I've tried doing the following in my_config.php
:
<?php
if(gethostbyaddr ("127.0.0.1") == 'hello.sabya'){
$this->config->load('config_production');
} else {
$this->config->load('config_development');
}
?>
It is not working as $this->config
is not initialized. How can I achieve this?
Upvotes: 2
Views: 5593
Reputation: 4976
Two options: You can try referencing the object with $CI
instead of $this
:
$CI =& get_instance(); //do this only once in this file
$CI->config->load();
$CI->whatever;
...which is the correct way to reference the CI object from the outside.
Or secondly, you could switch configs from within your config.php file:
<?php
if(gethostbyaddr ("127.0.0.1") == 'hello.sabya'){
$config['base_url'] = "http://dev.example.com/";
} else {
$config['base_url'] = "http://prod.example.com/";
}
?>
...etc. Load all the differences between the two if
/else
blocks.
Upvotes: 4
Reputation: 2310
This is old and already has an accepted answer but I have come up with a solution that I think works well. As @Eric mentioned, the best way to do this is to use the built-in functionality of the environment variable to determine the config logic. The problem I had this this approach is that it requires maintianing a lot of redundant values and you'll often want a central default config that gets run for all environments and only envrionment-specific values replace the defaults.
Your folder structure should be:
/app/config/config.php
/app/config/development/config.php
/app/config/production/config.php
Then, in your environment-specific config files, use the following as a starting point:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// Set the default values first, then we'll overwrite environmental-specific values
include(APPPATH . '/config/' . pathinfo(__FILE__, PATHINFO_BASENAME));
Using this as a starting point will let you do the exact same thing for database.php, etc. -- any /config/ file you want an environment specific version for without writing your own envrionment-loading logic that CI already provides.
Upvotes: 2
Reputation: 31
As Eric mentioned use environments.
You may load different configuration files depending on the current environment.
To create an environment-specific configuration file, create or copy a configuration file in application/config/{ENVIRONMENT}/{FILENAME}.php
Note: CodeIgniter always tries to load the configuration files for the current environment first. If the file does not exist, the global config file (i.e., the one in application/config/) is loaded. This means you are not obligated to place all of your configuration files in an environment folder − only the files that change per environment.
Why implement logic, when it's already there? ;)
Upvotes: 3
Reputation: 1197
Why not use environments? - http://codeigniter.com/user_guide/general/environments.html
Also the config files are just arrays. Why not just include it and merge it with the existing array? Or even better in index.php:
if (gethostbyaddr ("127.0.0.1") == 'production.example.com')
{
include(APPPATH.'config/config_production.php');
}
elseif (gethostbyaddr ("127.0.0.1") == 'staging.example.com')
{
include(APPPATH.'config/config_staging.php');
}
else
{
include(APPPATH.'config/config_development.php');
}
foreach ($config AS $key => $item)
{
$assign_to_config[$key] = $item;
}
But the best way is using environments in my opinion.
Upvotes: 0
Reputation: 4190
I use this in my case. For index.php
switch ($_SERVER['SERVER_NAME']) {
case 'localhost':
define('ENVIRONMENT', 'development');
break;
case 'example.com':
define('ENVIRONMENT', 'production');
break;
default:
define('ENVIRONMENT', 'production');
break;
}
For base_url in application/config.php:
switch ($_SERVER['SERVER_NAME']) {
case 'localhost':
$config['base_url'] = "http://localhost/my_app/";
break;
case 'example.com':
$config['base_url'] = "http://example.com/";
break;
default:
$config['base_url'] = "http://example.com/";
break;
}
For database in application/database.php:
switch ($_SERVER['SERVER_NAME']) {
case 'localhost':
$db['default']['username'] = 'root';
$db['default']['password'] = '123456';
break;
case 'example.com':
$db['default']['username'] = 'user';
$db['default']['password'] = 'pass';
break;
default:
$db['default']['username'] = 'user';
$db['default']['password'] = 'pass';
break;
}
Upvotes: 1
Reputation: 11884
I got it working like this: -
$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');
}
Upvotes: 0