I have a codigniter project with a custom config file called
application/config/my_config_variables.php:
This contains
<?php
$config['days'] = 20;
in :
application/config/autoload.php
I've 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?
addendum:
In my controller I've added:
// An alternate way to specify the same item:
$my_config = $this->config->item('my_config_variables',true);
var_dump($my_config);
this outputs FALSE -- why?
Look at the config documentation on Codeigniter.
echo $this->config->item('new_daily_contacts');
That will try to grab $config['new_daily_contacts']
but from your post it looks like you only have $config['days']
in your config file. But this should get you pointed in the right direction.
Alternate Way
With the alternate way you're trying to load the config, but you're using the item method, not the load method. On the item method, the 2nd parameter is the index you want to reference in the config array. For instance. 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');
Because passing true as the 2nd parameter on the load method will create an index with the same name as the config file. This helps avoid any conflicts if there happen to be other config files that contain the same config name.
$config['days'];
becomes $config['my_config_variables']['days']
In order to access that config variable you have to pass the index in the item method.
https://github.com/bcit-ci/CodeIgniter/blob/2.1-stable/system/core/Config.php#L189
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With