10

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?

3
  • What version of Codeigniter are you using? Commented Nov 11, 2014 at 19:13
  • CI 2.1 -- Thanks for looking at it. Commented Nov 11, 2014 at 19:55
  • stackoverflow.com/a/62427655/9573341 Commented Jun 17, 2020 at 11:14

1 Answer 1

19

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

Sign up to request clarification or add additional context in comments.

4 Comments

I've tried to follow this but still receiving errors - please see above:
Updated the answer with your alternate way.
Thanks for your help, $this->config->item('new_daily_contacts'); worked. BTW, I wasn't sure bwhat is meant by index, is this a key in an associative array, like $config['key'] ?
Ya, Codeigniter calls it an index in their documentation but it's a key. Same thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.