Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access custom config variable in codeigniter controller

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?

like image 349
user1592380 Avatar asked Nov 11 '14 18:11

user1592380


1 Answers

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

like image 120
slapyo Avatar answered Sep 19 '22 13:09

slapyo