Development Environments in CodeIgniter

Sarfraz Ahmed    April 21, 2015 05:06 PM

What is Development Environment?

The environment may be development, staging, production or some other. Each environment can have different set of settings; for example different application configurations, database settings, email settings, logging, error reporting levels and more. By using different environments for your application, you can actually ease things up for yourself when deploying as you won't have to change configurations, database setting and error reporting levels after application is moved to development or production site. Once you setup different development environments, the system would automatically know your application-wide settings to use.

If you have worked with Laravel, you would notice that you can setup different development environments for your application by specifying your current development environment in the .env file. You can actually have different development environments in CodeIgniter as well.

If you open up index.php file of CodeIgniter, you would notice it sets development environment as default in a constant ENVIRONMENT:

CodeIgniter 2+:

define('ENVIRONMENT', 'development');

CodeIgniter 3+:

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

And later in the very file, CI sets up error reporting based on that environment:

if (defined('ENVIRONMENT')) {
    switch (ENVIRONMENT) {
        case 'development':
            error_reporting(E_ALL);
            break;

        case 'production':
            error_reporting(0);
            break;

        default:
            exit('The application environment is not set correctly.');
    }
}

In fact, CI uses ENVIRONMENT constant at quite some places, quoting from its documentation:

There are some places in the CodeIgniter system where the ENVIRONMENT constant is used.


Setting Different Development Environments

Let's say we want to have ability to have different development environment for local, development and production. For example, we want to be able to:

  • Have error reporting turned on local and development environments but turned off on production
  • Have logging turned on local and development environments but turned off on production
  • Have different config settings for each of local, development and production environments
  • Have different database settings for each of local, development and production environments
  • Have different email settings for each of local, development and production environments

According to CI documentation, in order to setup different environments, you need to create a folder inside application/config folder matching the name of environment. For example, to create development environment, we would create a folder application/config/development.

So create three folders named development, local and production inside application/config folder. And copy config.php, database.php and email.php from application/config to three folders you just created. If email.php doesn't exist, you can create one.

Now each of your config.php, database.php and email.php can have differnt settings. CodeIgniter will automatically choose right files to use based on your current environment. For example in application/config/local/config.php and application/config/development/config.php files, you can enable error reporting and logging using $config['log_threshold'] setting by setting it to 1 but disable error reporting and logging in production environment by setting $config['log_threshold'] to 0 in application/config/production/config.php file. Similarly, you can now easily setup different $config['base_url'] in each of your environments' config.php file like:

application/config/local/config.php:

$config['base_url'] = 'http://localhost/mysite';

application/config/development/config.php:

$config['base_url'] = 'http://mysite.com/development';

application/config/production/config.php:

$config['base_url'] = 'http://mysite.com';

Now when you upload your application, all you have to do is specify which environment you want to use by editing index.php file:

define('ENVIRONMENT', 'production'); 

If you upload your application to production site, set it to production or if you upload it to development,set it to development.

I am sure you don't want to specify your environment for ENVIRONMENT each time you upload/deploy your application, here is how you can do so:

$local_servers = array('localhost');
$isLocal = in_array($_SERVER['SERVER_NAME'], $local_servers);

if ($isLocal) {
    $env = 'local';
} else {
    // check development word in url
    preg_match('#/development#', $_SERVER['REQUEST_URI'], $isDevelopment);

    $env = $isDevelopment ? 'development' : 'production';
}

And then use $env variable:

define('ENVIRONMENT', $env);







Comments powered by Disqus