Config Class
The Config class provides a means to retrieve configuration preferences. These preferences can come from the default config file (application/config/config.php) or from your own custom config files.
Note: This class is initialized automatically by the system so there is no need to do it manually.
Anatomy of a Config File
By default, Code Igniter has a one primary config file, located at application/config/config.php. If you open the file using your text editor you'll see that config items are stored in an array called $config.
You can add your own config items to this file, or if you prefer to keep your configuration items separate (assuming you even need config items), simply create your own file and save it in config folder.
Note: If you do create your own config files use the same format as the primary one, storing your items in an array called $config. Code Igniter will intelligently manage these files so there will be no conflict even though the array has the same name (assuming an array index is not named the same as another).
Loading a Config File
Note: Code Igniter automatically loads the primary config file (application/config/config.php), so you will only need to load a config file if you have created your own.
There are two ways to load a config file:
- Manual Loading
To load one of your custom config files you will use the following function within the controller that needs it:
$this->config->load('filename');
Where filename is the name of your config file, without the .php file extension.
- Auto-loading
If you find that you need a particular config file globally, you can have it loaded automatically by the system. To do this, open the autoload.php file, located at application/config/autoload.php, and add your config file as indicated in the file.
Fetching Config Items
To retrive an item from your config file, use the following function:
$this->config->item('item name');
Where item name is the $config array index you want to retrieve. For example, to fetch your language choice you'll do this:
$lang = $this->config->item('language');
The function returns FALSE (boolean) if the item you are trying to fetch does not exist.
Setting a Config Item
If you would like to dynamically set a config item or change an existing one, you can so so using:
$this->config->set_item('item_name', 'item_value');
Where item_name is the $config array index you want to change, and item_value is its value.
Helper Functions
The config class has the following helper functions:
$this->config->site_url();
This function retrieves the URL to your site, along with the "index" value you've specified in the config file.
$this->config->system_url();
This function retrieves the URL to your system folder.