Skip to content

Environment driven database settings in CodeIgniter?

Have you ever developed your application locally, then taken it live on your production server and had to manually change your app configuration to match the production server? If you have, then continue reading and learn how configuring environments in CodeIgniter can save you from this headache.

Environments

Software projects are typically deployed in various environments. You likely develop your application locally in a development environment. You then have your live server where your site resides, known as the production environment. Many applications will have a staging environment, essentially a duplicate of your production environment, where apps are deployed and tested before being pushed to production. Larger applications may have other environments in between.

Each of these environments may require different configuration, and luckily we can easily handle that in CodeIgniter.

Setp1 : go to index.php file add the fallowing switch statement

switch (dirname(__FILE__)) {
    case 'c:htdocshtdocs':
        define('ENVIRONMENT', 'development');
        break;
    case '/arjun.net.in/public_html/staging/':
        define('ENVIRONMENT', 'staging');
        break;
    default:
        define('ENVIRONMENT', 'production');
        break;
}

Then again add on other switch statement to pick current environment type.

switch (ENVIRONMENT) {
    case 'development':
        $active_group = 'development';
        break;
    case 'staging':
        $active_group = 'staging';
        break;
    default:
        $active_group = 'production';
        break;
}


$active_record = TRUE;

$db['development']['hostname'] = 'localhost';
$db['development']['username'] = 'development';
$db['development']['password'] = 'development';
$db['development']['database'] = 'development';
$db['development']['dbdriver'] = 'mysql';
$db['development']['dbprefix'] = '';
$db['development']['pconnect'] = FALSE;
$db['development']['db_debug'] = TRUE;
$db['development']['cache_on'] = FALSE;
$db['development']['cachedir'] = '';
$db['development']['char_set'] = 'utf8';
$db['development']['dbcollat'] = 'utf8_general_ci';
$db['development']['swap_pre'] = '';
$db['development']['autoinit'] = TRUE;
$db['development']['stricton'] = TRUE;

$db['staging']['hostname'] = 'localhost';
$db['staging']['username'] = 'staging';
$db['staging']['password'] = 'staging';
$db['staging']['database'] = 'staging';
$db['staging']['dbdriver'] = 'mysql';
$db['staging']['dbprefix'] = '';
$db['staging']['pconnect'] = FALSE;
$db['staging']['db_debug'] = FALSE;
$db['staging']['cache_on'] = FALSE;
$db['staging']['cachedir'] = '';
$db['staging']['char_set'] = 'utf8';
$db['staging']['dbcollat'] = 'utf8_general_ci';
$db['staging']['swap_pre'] = '';
$db['staging']['autoinit'] = TRUE;
$db['staging']['stricton'] = FALSE;

$db['production']['hostname'] = 'localhost';
$db['production']['username'] = 'production';
$db['production']['password'] = 'production';
$db['production']['database'] = 'production';
$db['production']['dbdriver'] = 'mysql';
$db['production']['dbprefix'] = '';
$db['production']['pconnect'] = FALSE;
$db['production']['db_debug'] = FALSE;
$db['production']['cache_on'] = FALSE;
$db['production']['cachedir'] = '';
$db['production']['char_set'] = 'utf8';
$db['production']['dbcollat'] = 'utf8_general_ci';
$db['production']['swap_pre'] = '';
$db['production']['autoinit'] = TRUE;
$db['production']['stricton'] = FALSE;

You can also set database connection values for specific environments by placing database.php it the respective environment config folder. For Example : create new folders with your environment names in config folder then copy past the default database.php file in that, then change the settings. Follow the bellow folder structure.
application/config/production/database.php
application/config/development/database.php
application/config/staging/database.php

0 0 votes
Article Rating
Subscribe
Notify of
guest

1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments