Pages

Monday, December 30, 2013

Create a Custom config file for Codeigniter


By default, CodeIgniter has one primary config file, located at “application/config/config.php
What if we want to have a custom configuration file?  What if we want to create custom config array with desired name?


Codeigniter allows a developer to create custom file but developer has to define values with $config array developer cannot define a custom variable or array inside this new file and use it

$this->message->item(‘msg_cn_welcome’);

By doing this developer will get an error - “application/config/custom.php file does not appear to contain a valid configuration array.

Well to over come with this, I have created a customized message configuration file that will store all messages at one place. This article will help you to create your own custom config files.

At the end of this article downloadable packages are available for complete codeigniter v2.1.4 with customized files and only customized files.

Step 1: Create your config file.  

I have created a file called message.php in “application/config/" directory. This file contains a config array named “message”.  Define as much as data in $message array, that you would like to access.

$message['msg_cn_welcome']                  = "Welcome to CodeIgniter!";   


Step 2: Create a Class

Create a Message Class in "/system/core/ ". This is going to be a core class as it will be auto loaded. Define a Method inside this class to perform a desire action. With respect to Message class, I have define a method calleditem. Item function will get the value from $message array and will return it to the caller function.   

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CodeIgniter
*/
class CI_Message {

    var $message = array();
    var $is_loaded = array();
   
    function CI_Message()
    {
        $this->message =& get_message();
        log_message('debug', "Message Class Initialized");
    }
     
    function item($item, $index = '')
    {   
        if ( ! isset($this->message[$item]))
        {
            return FALSE;
        }

        $pref = $this->message[$item];
       
        if ( isset($index) OR is_array($index))
        {   
            if(is_array($index))
            {
                for($i=1; $i <= sizeof($index); $i++)
                {
                    $pref = str_replace('%V'.$i,$index[$i-1],$pref);
                }
            }
            else
            {
                $pref = str_replace('%V',$index,$pref);
            } 
        }
        return $pref;
    }
} 
 

Step 3 : Create a reference function to load this new config file.

Now create a reference function get_message in “Common.php” file located at "/system/core/ ".
This function will be responsible to load message.php config file and read $message array and its values.   Here is the code block.

/**
* Loads the main message.php file
*  Added By  : Pinal Desai
* @access    private
* @return    array
*/
function &get_message()
{
    static $main_msg;

    if ( ! isset($main_msg))
    {
        if ( ! file_exists(APPPATH.'config/message'.EXT))
        {
            exit('The configuration file message'.EXT.' does not exist.');
        }

        require(APPPATH.'config/message'.EXT);
       
        if ( ! isset($message) OR ! is_array($message))
        {
            exit('Your Message file does not appear to be formatted correctly.');
        }

        $main_msg[0] =& $message;
    }
    return $main_msg[0];
}

Step 4 : Load the Class

Now one thing is remaining, which is; to load a Message class. To load this customized class; add following code block to “CodeIgniter.php” file located at "/system/core/ ".


/*
* ------------------------------------------------------
*  Load the Message class
* ------------------------------------------------------
*/

$EMSG =& load_class('Message', 'core');


Step5: Use the custom cofig file


Ready to use newly created config file and array. Following code will show the simple usage.

echo $this->message->item(‘msg_cn_welcome’);


Download DEMO Package:


Complete Codeigniter v2.1.4:  Download Now
Only Customized Code and Files : Download Now

Article by: Pinal Desai


No comments:

Post a Comment