Doyenhub

Create a Custom config file for Codeigniter

 

How to Create a Custom Config File in CodeIgniter

By default, CodeIgniter provides a primary configuration file located at:
application/config/config.php

But what if you want to create a custom configuration file with a custom config array? While CodeIgniter allows developers to create custom files, certain rules must be followed. Specifically, you must define values using the $config array; defining custom variables or arrays outside of this structure will cause errors, such as:

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

Error:
"application/config/custom.php file does not appear to contain a valid configuration array."

To solve this issue, this guide demonstrates how to create a custom configuration file that works seamlessly within CodeIgniter. By the end, you’ll have a fully functional example and downloadable packages for both a complete CodeIgniter v2.1.4 setup and only the customized files.

Steps to Create a Custom Config File in CodeIgniter

Step 1: Create Your Config File

Create a file named message.php in the application/config/ directory. This file will contain a $message array to store your custom configuration values.

Example:

$message[‘msg_cn_welcome’] = “Welcome to CodeIgniter!”;

Step 2: Create a Core Class

Create a Message class in the /system/core/ directory. This core class will be auto-loaded and handle retrieving values from the $message array.

Example:

<?php if (!defined(‘BASEPATH’)) exit(‘No direct script access allowed’);

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) && is_array($index)) {
for ($i = 1; $i <= sizeof($index); $i++) {
$pref = str_replace(‘%V’ . $i, $index[$i – 1], $pref);
}
}

return $pref;
}
}

Step 3: Create a Reference Function

Add a get_message() function in the Common.php file located at /system/core/. This function will load the message.php config file and its $message array.

Example:

function &get_message() {
static $main_msg;

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

require(APPPATH . ‘config/message.php’);

if (!isset($message) || !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

To auto-load the Message class, add the following code to CodeIgniter.php in the /system/core/ directory:

$EMSG =& load_class(‘Message’, ‘core’);

Step 5: Use the Custom Config File

You can now use your custom configuration file and array in your application.

Example:

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

Downloadable Demo Packages

To make it easier, download the pre-configured setup:

Conclusion

Creating a custom configuration file in CodeIgniter helps streamline your project by centralizing values like messages or settings in one place. By following this guide, you can implement custom config files without encountering errors and ensure your application is easier to manage and extend.

 

Leave a comment

Your email address will not be published. Required fields are marked *