codeigniter: access config variables from other config files?
I have two config files.
config.php (code igniters core config)
and email.php (autoloaded by the email class when it is used)
What i am wanting to do is.
In config.php have
$config['env'] = 'hailwood_dev';
then in email.php have
if($config['env'] == 'hailwood_dev'){
//email variables like smtp server to do with localhost
} elseif($config['env'] == 'producti开发者_StackOverflowon'){
//email variables like smtp server to do with production
}
But this is not having any effect (im guessing as $config['env'] does not have those values).
How can I access this value?
I checked and at that point in the request lifecycle the config property of the CodeIgniter object is just an array, not a config object.
So you should be able to get at your configuration setting like this :
$this->config['env']
So this should work :
if ($this->config['env'] == 'hailwood_dev')
{
//email variables like smtp server to do with localhost
}
elseif ($this->config['env'] == 'production'){
//email variables like smtp server to do with production
}
If you are autoloading configuration files, make sure they are autoloaded in the correct order. A configuration file must be autoloaded after any it depends on.
Add this to your email config file.
$environment = config_item('env');
OR THIS
$environment = $this->config->item('env');
Not sure if the first will work on your setup. Most people seem to use the second.
Try this:
$env = $this->config->item('env');
if ($env == "dev_server") {
// Do this...
}
else {
// Do this..
}
In this case you would want to put configuration in
- application/config/ceveloppment/my_config.php
- application/config/production/my_config.php
http://codeigniter.com/user_guide/libraries/config.html
Combining with other awnsers you could share information between two configuration files for the same environnement
use config_item function (system/core/Common.php)
/** * Returns the specified config item * * @access public * @return mixed
Please see below solutions
$this->config->config['base_url']
echo "<pre>";
print_r($this->config);
Above will print required details
In CodeIgniter 3.0 the solution to accessing config variables in another config file is to get the CodeIgniter instance. Once you have a reference to it you can then access using the item
function.
$CI =& get_instance();
$var = $CI->config->item('item_name');
精彩评论