Create custom config file in codeigniter

Rakesh Sharma      1 Comment  

How to create custom config file in codeigniter and fetch config items

We need to create custom config file in codeigniter to enhance or add some more config items. In codeigniter we have already a config file in(application/config/config.php) we can add more items directly in it or can create our own custom config file. Altering existing config file is not a good idea so we need to create a new config file. In codeigniter all config settings are in $config array. if you look into inbuilt config file you will found all config stored in $config array. below we will see an example of how to create custom config file in codeigniter.

Creating custom config file :-

1. 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['author_email'] = '';
$config['author_page'] = 'profile';

you need to add your items or config values in $config array syntax:-

$config['your_key'] = 'Your_config_value';

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

1. Manual loading :- we can manual load config file in controller/model like

$this->config->load('file_name'); //or use custom_config instead of file_name

where “file_name” is your custom config file name without .php extension.

2. Autoloading :- for auto load config file go to “application/config/autoload.php” and add code in $autoload[‘config’]

$autoload['config'] = array('config_custom'); //where config_custom is your file name.

Fetching config file items :-

After load we can get easily loaded config file items in controller/models/views by simply call a function. we need to only pass item name for fetch it.

// For our created items like

you can also set or change config item values on run time like

$this->config->set_item('item_name', 'item_value');

Now you are all set with create custom config file in codeigniter above we have discussed about create custom config file, load custom config, fetching config items in codeigniter.

  • Eduardo Azevedo