Setup a local configuration file in Codeigniter « Web Development

McLaughlin Casey "Setup a local configuration file in Codeigniter." Casey A. McLaughlin's Weblog. Posted 29 Apr 2011. Retrieved 23 Feb 2012(http://www.caseymclaughlin.com/2011/04/setup-a-local-configuration-file-in-codeigniter/)

Even if you have twenty local environments on //one freaking computer//, this technique still works :)After four years of development, I’m still a big CodeIgniter fan, although some other frameworks are starting to look mighty interesting.  Despite the bugs in Version 2.0.2, however, CI is heading in the right direction.

One CI-related issue that has caused problems for my workplace in the past has been the ability to setup distributed development environments.  Everybody has a different local setup, and some of the configuration directives inside of CI’s application/config folder must change from machine to machine.

Of course, we can’t just tell Mercurial to ignore that folder during commits, because there are some configuration directives that should remain the same across all environments, such as language, charset, enable_hooks, etc.

So, how to make some configuration settings consistent across different computers, and how to make some local settings?

Enter: config.local.php.

The solution is to create a local configuration file that overrides default config.php settings.  This way, local environments can set certain settings locally, and use the defaults in the application/config folder by default.  Here’s how to do it:

Step One: Create a local configuration file, and add certain configuration settings that you want to override.  You can put it in your root folder, and name it ./config.local.php For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
/* Local Configuration Options */
$config["base_url"] = "http://localhost/projects/my_project/";
$config["index_page"] = "index.php";
$config["uri_protocol"] = "AUTO";
$config["email_protocol"] = "sendmail";
$config["mailpath"] = "/usr/bin/sendmail";
$config["smtp_host"] = "";
$config["smtp_user"] = "";
$config["smtp_pass"] = "";
$config["smtp_port"] = "25";
$config["smtp_timeout"] = "5";
$config['log_threshold'] = 4;
//..and whatever else you want to override..
 
/* EOF */

Step Two: Set your version control system to ignore this file.  If you’re using Mercurial, simply add a line to the .hgignore file:

1
^config\.local\.php$

Step Three: Subclass the core/Config.php library to read the local configuration file after reading the default one every time.  To do this, create the file application/core/MY_Config.php in your application folder, and add the following code inside that file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class MY_Config extends CI_Config
{
	/**
	 * Load a config file - Overrides built-in CodeIgniter config file loader
	 * 
	 * @param string $file
	 * @param boolean $use_sections
	 * @param boolean $fail_gracefully 
	 */
	function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
	{
		parent::load($file, $use_sections, $fail_gracefully);
 
		//Local settings override permanent settings always.
		if (is_readable(FCPATH . 'system/config.local.php'))
			parent::load('system/config.local.php', $use_sections, $fail_gracefully);
	}
}
 
/* EOF: MY_Config */

Using local settings for database Configuration

Another file that is likely to be completely different from one environment to the next is the application/config/database.php file.  Since CodeIgniter seems to read the database settings a bit differently than normal settings, we have to go through one extra step to override those.  It’s a bit messier than I’d like, but gets the job done:

Step One: Add a line to your config.local.php for each database setting you want to override, but put the database settings in their own sub-array:

1
2
3
4
5
6
7
//...inside the local config file...
$config["db"]["hostname"] = "localhost";
$config["db"]["username"] = "localuser";
$config["db"]["password"] = "";
$config["db"]["database"] = "oeg_dev";
$config["db"]["db_debug"] = "TRUE";
$config["db"]["dbdriver"] = "mysql";

Step Two: Add the following code to the end of the application/config/database.php file to read those settings after reading the default settings:

1
2
3
4
5
6
7
//If there is a local config file, overwrite the settings with that..
if (is_readable(FCPATH . 'config.local.php'))
{
	include_once(FCPATH . 'config.local.php');
	foreach($db['default'] as $key => $val)
		$db['default'][$key] = (isset($config['db'][$key])) ? $config['db'][$key] : $val;
}

There you have it!  From now on, you can your local configuration file to override any configuration setting in CodeIgniter.  This makes sharing code a breeze.  You developers can set their own base_path, database, index_file and other settings that are likely to change from one environment to the next without modifying the main configuration files.

PS. Happy Friday!

PPS. Have a better method? I’d love to hear about it.

Comments

3 Responses to “Setup a local configuration file in Codeigniter”
  1. You have a nice technique up there. But i have another solution and i don’t know if it is better than your solution.
    I’m using “ENV” constant which i delcared in constants.php.
    ENV value can be local, dev, qa and live.
    I develop a simple script to switch between them (i put it in constants.php),
    if my SERVER_NAME contain localhost, then ENV=’local’, if SERVER_NAME contain development.* then ENV=’dev’ and so on.

    And in my database setting have it like this:
    …..
    $db['local']['hostname'] = ‘localhost’;
    $db['local']['username'] = ‘root’;
    $db['local']['password'] = ”;
    $db['local']['database'] = ‘mydb’;
    $db['local']['dbdriver'] = ‘mysql’;
    ……
    $db['dev']['hostname'] = ‘localhost’;
    $db['dev']['username'] = ‘root’;
    $db['dev']['password'] = ”;
    $db['dev']['database'] = ”;
    $db['dev']['dbdriver'] = ‘mysql’;
    ………… so on ……

    so when i move my script to another host it will automatically detect the environment and load the settings for me.

    Well i’m just a begginner in CodeIgniter and i love to read your blog about CI to improve my skill.

    Thanks

    Ivan

  2. Jerico says:

    Great read, thanks!

  3. Muhammad Furqan Freed says:

    Hi
    this is interesting technique but a bit lengthy process..
    i am also doing the same technique with multiple constants define. but i need to do some thing else

    i have a scnerio: i have multiple clinets for same project, whta i want to do is to include app_config flie (own defined config file) which is different for each client.. its mean i have to make app_config and constant files that very from client to client…

    so what should i do? should i extebd and use MY_ or ?

    thank you in advance.

Speak Your Mind

Add a comment below...
and oh, if you want a pic to show with your comment, go get a gravatar!

Better Tag Cloud