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:
- not every host can set environment variables in .htaccess, also this won’t work on non-apache web servers
- it still requires you to modify a project file that could be checked into source control
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']);
case ‘mysite.com’ :
case ‘stage.mysite.com’ :
//our staging server
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');
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.
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.
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:
- CodeIgniter multiple environments are configured by setting the ENVIRONMENT PHP constant
- By default the ENVIRONMENT constant is set in index.php, which makes it difficult in practice to keep environment configurations separate
- By creating an environments.php file w/ code to check the HTTP_HOST server variable, we can set the ENVIRONMENT variable based on the web project URL.
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!