Reputation: 567
So I was reading in the Codeigniter user guide and they say that:
You can also pass parameters stored in a config file. Simply create a config file named identically to the class file name and store it in your application/config/ folder. Note that if you dynamically pass parameters as described above, the config file option will not be available.
I have a config file set up with the identical name as my library and I am wondering how I can access the contents of that file. Do I need to load the config before loading the lib? Do all of the variables need to be elements of an array?
examples:
-- Config file --
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$var1 = "Variable 1";
$var2['name'] = "foo";
$var2['title']= "bar";
$var2['content'] = "Lorem ipsm dolar sit imut";
-- Library file --
class Library
{
var $data;
function __construct($data)
{
$this->data = $data;
}
}
-- Then something along the lines of --
function __construct() {
parent::__construct();
$this->load->library("library");
}
function index()
{
var_dump($this->library->data);
}
(Yes I figure the name Library is reserved but this is an example)
Shouldn't the var_dump simply dump the contents of the config file? What am I doing wrong?
Upvotes: 1
Views: 4224
Reputation: 3367
Few things to make this work :
Your classname FILE should start with a capital letter, Your config array must be defined as $config = array()
(and not $var
or any other name), Your config file must be lowercase.
The config file is passed through the constructor, so class($config=array())
, you can then access and store the config variables on an instance variable.
hope it helps:
class Foo // Foo.php
{
function __construct($config=array())
{
$this->config = $config;
}
} //end of class
config file:
$config['url'] = 'stuff'; // config file : foo.php
good luck :)
Upvotes: 2
Reputation: 3125
To access the variables of config file in your custom library, you can initialize a new CI instance and access the content of config like below:
$this->ci = & get_instance();
$configs = $this->ci->config;
Now do var_dump($configs)
to see content of config file.
Upvotes: 0