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?
To load one of your custom config files you will use the following function within the controller that needs it: $this->config->load('filename');
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');
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');
It seems like you missed an underscore for your constructor:
instead of
public function _construct () {
you should use
public function __construct () {
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