Handling Multiple Environments In CodeIgniter

If you have been developing and deploying websites using the CodeIgniter framework chances are that you have come across the need to switch your app's environment settings. If you are looking for a way to automate that process you have come to the right place. There have been a number of questions and suggestions posted on StackExchange requarding this topic like this one that do not completely automate the process. The one presented in this article does, that is given that you use a dev, staging, production environment setup and have your vhost file configured appropriately.

Why might you need to switch the environment constant? Well because you are switching environments silly. You don't want to display server sensitive information to the end user in a production environment.

When I was a newbie I thought like a newbie, now that I am a professional developer I think like one! — Myself

See EllisLab's answer:

Developers often desire different system behavior depending on whether an application is running in a development or production environment. For example, verbose error output is something that would be useful while developing an application, but it may also pose a security issue when "live" — EllisLab.

As stated in my introduction, there have been a number of approaches to automate this but most have come short for my needs. That is, before I stumbled on the one created by Jed Kirby. The reason I prefer his method over others is that it can be used and reused for any site. So as long as you prefix or suffix your host url appropriately. Kirby assumed that you are used to a dev, staging, production environment setup and have your vhost file configured appropriately. However, he assumes that you use the prefix or suffix of dev, or stage. Where most of the developers I know uses local, dev, or staging (including myself). See my fork here with added support for those who use the ones I mentioned.

Index.php Sample:

if ( ! function_exists('domains_determine_uri'))
{
    function domains_determine_uri($domain_environment = 'prefix')
    {
        $http_host = (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : FALSE);
        $http_split = ($http_host ? explode('.', $http_host) : FALSE);
        switch(strtolower($domain_environment))
        {
            case 'prefix':
                $domain_uri = $http_split[0];
                break;
            case 'suffix':
                $domain_uri = end($http_split);
                break;
            default:
                exit('The domain environment has not been set correctly, please use either prefix or suffix.');    
        }
        return (isset($domain_uri) ? $domain_uri : FALSE);
    }
}

...

$domains['environment'] = 'prefix';
switch( domains_determine_uri( $domains['environment'] ) )
{
    case 'dev':
    case 'local':
        define('ENVIRONMENT', 'development');
            $system_path = 'system';
            $application_folder = 'application';
        break;
    case 'stage':
    case 'staging':
        define('ENVIRONMENT', 'staging');
            $system_path = 'system';
            $application_folder = 'application';
        break;        
    default:
        define('ENVIRONMENT', 'production');
            $system_path = 'system';
            $application_folder = 'application';
}

The code above would be added to your root index.php file above the if (defined('ENVIRONMENT')) line.

Host URL Example:

local.mywebsite.com or mywebsite.local
dev.mywebsite.com or mywebsite.dev
staging.mywebsite.com

Hope this helps you auto-switch your environment.

Location

Anderson, SC

Phone

864.559.8253