Custom config file for codeigniter
Creating custom config file: Add a new file under “application/config/” with named “custom_config.php” (or give any name) and add below code
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
//adding config items.
$config['gender'] = array('female', 'male');
Loading custom config file :- After create custom config file we need to load it get it’s items. for load custom config we have two ways
*** Manual loading:- we can manual load config file in controller/model like
$this->config->load('custom_config'); //or instead your file name.
*** Autoloading :- for auto load config file go to “application/config/autoload.php” and add code in $autoload[‘config’]
$autoload['config'] = array('custom_config'); //or instead your file name.
CodeIgniter 3
Step 1: Create the custom configuration file
/path/to/codeigniter/application/config/custom.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$config['server'] = array (
'host' => 'example.com',
'public_ip' => '93.184.216.34'
);
?>
Step 2: Load the custom configuration file
This can be done either inside a model or a controller.
// load the configuration file
$this->config->load('custom');
// retrieve the content of a specific configuration
$server = $this->config->item('server');
// $server is now set
echo $server['host']; // example.com
Use
$config['gender']= array ('male','female');
instead of
$gender = array ('male','female');
For fetching config item
$this->config->item('item_name');
Where item_name
is the $config
array index you want to retrieve.
Docs : CodeIgniter User Guide 2.x CodeIgniter User Guide 3.x