Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading custom config file into a Codeigniter library

I know this is probably simple, but I'm not getting. I've created a library, and I want to load the parameters from a config file. So here's an example of what I have:

// libraries/Mylib.php
class Mylib {
   var $ci;
   var $key;
   public function _construct {
     $this->ci =& get_instance();
     $this->ci->config->load('mylib');
     $this->key = $this->ci->config->item('key');
   }
   public function myKey() {
     return "Key=" . $this->key;
   }
}

// config/mylib.php
$config['key'] = 'randomcharacters';

I load the library, and try to print out the myKey function, but it just returns "Key=", without the actual key. What am I missing?

like image 611
TerryMatula Avatar asked Oct 06 '10 15:10

TerryMatula


People also ask

How install custom config file in CodeIgniter?

To load one of your custom config files you will use the following function within the controller that needs it: $this->config->load('filename');

How get data from config file in CodeIgniter?

Getting config variable value: The config items can also be easily fetched from the CodeIgniter environment. The config class function item() is used to get the value of a config variable in Codeigniter. Syntax: $this->config->item('item_name');

How you will use or load CodeIgniter libraries?

All of the available libraries are located in your system/libraries/ directory. In most cases, to use one of these classes involves initializing it within a controller using the following initialization method: $this->load->library('class_name');


1 Answers

It seems like you missed an underscore for your constructor:

instead of

public function _construct () {

you should use

public function __construct () {
like image 178
mseo Avatar answered Oct 04 '22 05:10

mseo