How to Setup CodeIgniter Multiple Environments

Updated: 2017-02-12 – WARNING!! using $_SERVER[‘HTTP_HOST’] is NOT SAFE, it can be edited by the client DO NOT USE IT

Instead I would recommend taking a look at phpdotenv to define environment variables to pass into CodeIgniter

Setting up CodeIgniter multiple environments is fairly easy and supported by default in the framework. However it requires you to define your environment in a core file instead of setting it up to recognize the url which can cause several issues. Fortunately a simple technique can be used to extend CodeIgniter multiple environments to give it more flexibility.

CodeIgniter, while not as new as other PHP frameworks, is still a great platform for developing client web applications. One thing we do for every project at Anecka is configure a local, stage and production environment. This helps us a lot when we have to maintain the project after launch as we have separate environments for developing and testing features. CodeIgniter is great because it supports multiple environments out of the box.

You can find out more about setting up multiple environments in CodeIgniter by reading the official docs here.

Environments in CodeIgniter are set using the PHP constant ENVIRONMENT. If you open index.php (https://github.com/EllisLab/CodeIgniter/blob/develop/index.php) you can see near the top where the environment constant is set:

define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');

As you can see the ENVIRONMENT constant is set depending on the “CI_ENV” server variable. You can set this variable in the .htaccess file in your project’s public folder.

SetEnv CI_ENV development

Unfortunately this has several drawbacks:

What’s needed is a way to detect the environment by the $_SERVER[‘HTTP_HOST’] server variable, this is the host used to access the application. For example, if the user accesses the site using http://mysite.com, then HTTP_HOST will be “mysite.com.”

For our project we use a file called “environments.php” to set the ENVIRONMENT constant. Here’s what it looks like:

if(! defined('ENVIRONMENT') )
{
$domain = strtolower($_SERVER['HTTP_HOST']);

switch($domain) {
case ‘mysite.com’ :
define(‘ENVIRONMENT’, ‘production’);
break;

case ‘stage.mysite.com’ :
//our staging server
define(‘ENVIRONMENT’, ‘staging’);
break;

default :
define(‘ENVIRONMENT’, ‘development’);
break;
}
}

To use this file, we comment out the old “define(‘ENVIRONMENT’,….)” line and replace it with:

//define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');
require('environments.php');

Now that we have the environment set by URL, we can fully take advantage of CodeIgniter’s multiple environment features.

You can create environment specific folders under /application/config to override CI’s default configuration settings.

codeigniter multiple environments

Under the “development” folder we have a database.php file that we DON’T check into our source control repository. This allows developers to set the project to use the database configuration settings unique to their own environments without affecting the rest of the team.

codeigniter multiple environments

In addition to database.php, /application/config/config.php can be overridden by copying it to one of the environment specific files and changing the values for that environment. CodeIgniter will check for a configuration setting in the environment folders first, then if it can’t find the setting it will look for it in the default configuration file.

What we learned about CodeIgniter Multiple Environments:

Thanks for reading! I hope this helps you in your next (or current) CodeIgniter project. If you have any techniques of your own or questions please share them in the comments! Also we’re doing a whole series on multiple environment configuration in various platforms and content management systems. If you have a CMS or platform you want to see featured, drop me a note or send me an email!