Hailwood
Hailwood

Reputation: 92581

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'] == 'production'){
//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?

Upvotes: 5

Views: 15087

Answers (7)

Daniel Waghorn
Daniel Waghorn

Reputation: 2985

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');

Upvotes: 0

Duke
Duke

Reputation: 36970

Please see below solutions

$this->config->config['base_url']

echo "<pre>";
print_r($this->config);

Above will print required details

Upvotes: 0

Youssef
Youssef

Reputation: 3114

use config_item function (system/core/Common.php)

/** * Returns the specified config item * * @access public * @return mixed

Upvotes: 0

Guillaume Mass&#233;
Guillaume Mass&#233;

Reputation: 8064

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

Upvotes: 0

Stephen Curran
Stephen Curran

Reputation: 7433

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.

Upvotes: 7

Anthony Jack
Anthony Jack

Reputation: 5553

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.

Upvotes: 6

mdgrech
mdgrech

Reputation: 955

Try this:

 $env = $this->config->item('env');

 if ($env == "dev_server")  {
   // Do this...
 }
 else  {
   // Do this..
 }

Upvotes: 3

Related Questions