Elroyjetson

Codeigniter Environments

Posted on: by

Introduction

Have you ever wished that you could have a separate set of configuration files for your different development environments? My usual setup consists of a development environment, usually on my local machine, a staging server where clients can view the work prior to going live, and a production server. Each of these three environments have slightly different aspects to them that I need to change to migrate the work between each of them.

Wouldn’t it be nice if I could just tell my Codeigniter application which environment it is on and then use the set of configuration files appropriate to that environment?

It turns out that this ability is already baked into Codeigniter and it is really simple to use.

Set the ENVIRONMENT

As I indicated previously, I have three environments, development, staging, and production so I will use these as my environment labels. To tell your Codeigniter application which environment you are on, locate your main index.php file, I usually keep mine in a /public/ folder which becomes my DOCUMENT_ROOT so that my application is outside of my web site.

Inside this file locate the first editable line in the APPLICATION ENVIRONMENT code block. It should look like this:

define('ENVIRONMENT', 'development');

By default it is already set to development, but depending on the environment you can change this to suit your needs.

That was really the hard part, the rest is easy.

Environment config files

Now that we have set the ENVIRONMENT constant, we have to create our config files for each of these environments. To do this head into your application and then config folder. Remember that I said I had three environments, development, staging, and production. So in the config folder within my Codeigniter application I will create three directories one to match each of these environments. The structure should look similar to this

/application
    /config
        /development
        /staging
        /production

The way Codeigniter works, is that it follows a hierarchy for loading config files. Codeigniter starts by checking the ENVIRONMENT constant, if it is set, it takes that value and looks to see if there is a corresponding directory to match in the config directory, if so, it loads those files, if not it falls back to the default location in the config directory and loads that file instead. What this means is that you only have to place the config files that are different into the environment directories.

The two most common files I place in these directories are the main config.php and database.php. You can place any configuration file that you need to in these directories or even your own application specific config files.

Conclusion

It is as simple as that to now have the flexibility to have one code base that you can deploy easily between each of your three environments without having to swap out files depending upon the server you are deploying to.

More Information

  1. Handling Multiple Environments
  2. Config Class

Updated: