How to access custom configuration variable in codeigniter controller

I have a codigniter project with a custom configuration file named

application/config/my_config_variables.php: 

It contains

 <?php $config['days'] = 20; 

in:

  application/config/autoload.php 

I added:

 $autoload['config'] = array('my_config_variables'); 

when I try to access this in my controller using:

  echo $this->$config['new_daily_contacts']; 

I get:

 Fatal error: Cannot access empty property. 

What am I doing wrong?

Application:

In my controller, I added:

  // An alternate way to specify the same item: $my_config = $this->config->item('my_config_variables',true); var_dump($my_config); 

it outputs FALSE - why?

+7
php codeigniter
source share
1 answer

See the configuration documentation on Codeigniter.

 echo $this->config->item('new_daily_contacts'); 

This will try to capture $config['new_daily_contacts'] , but from your post, it looks like you only have $config['days'] in your configuration file. But that should make you point in the right direction.

Alternative way

With an alternative way, you try to load the configuration, but use the item method, not the load method. In the item method, the second parameter is the index that you want to reference in the config array. For example. With your example above.

 $my_config = $this->config->load('my_config_variables', true); var_dump($my_config); $days = $this->config->item('days', 'my_config_variables'); 

Since passing true as the second parameter of the boot method will create an index with the same name as the configuration file. This helps to avoid conflicts if there are other configuration files that contain the same configuration name.

$config['days']; becomes $config['my_config_variables']['days']

To access this configuration variable, you need to pass the index in the item method.

https://github.com/bcit-ci/CodeIgniter/blob/2.1-stable/system/core/Config.php#L189

+13
source share

All Articles