CodeIgniter Multiple Environment configuration

in codeigniter have many environtment,

  • development
  • testing
  • production

as far I know, this environment describe ‘how security is’,

environment development it’s default echo warning and error.

environment production and testing said nothing about that, in hope user cannot find ‘hole’ application

in real web project, me always have at least 2 environment, localhost as development and client hosting as production

they have different configuration, example database configuration have user, password and database from other environment

or maybe environment have specific configuration like  ‘autoload, config, hooks etc’

see: http://ellislab.com/codeigniter/user-guide/libraries/config.html#environments

Example multiple configuration for multiple environment

  1. open file your_ci_folder/index.php see line 21
    define('ENVIRONMENT', 'development');
    
  2. go to your_ci_folder/application/config path
  3. create folder development and production
  4. copy file your_ci_folder/application/config/database.php
  5. to your_ci_folder/application/config/production/database.php
  6. and to your_ci_folder/application/config/development/database.php
  7. remove file your_ci_folder/application/config/database.php
  8. edit your file your_ci_folder/application/config/development/database.php to your database environment, my example database configuration development
  9. edit your file your_ci_folder/application/config/development/database.php to your database environment, and production to production database environment

my example database configuration development
development (localhost)

$active_group = 'default';
$active_record = TRUE;

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

production

$active_group = 'default';
$active_record = TRUE;

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

structure folder

codeigniter config multiple environment
codeigniter config multiple environment

Auto detect and switch Environment Configuration

if you read index.php at line 21, you notice the environment manually declaration.

you can add line script to get what environment now, the script detect what path index.php

open index.php change line

define('ENVIRONMENT', 'development');

to

switch(dirname(__FILE__)) {
case '/home/www/localhost_ci_folder': // development
define('ENVIRONMENT', 'development');
break;
default: // production
define('ENVIRONMENT', 'production');
break;
}

Published by

G3n1k

just to remember what i had known :)

One thought on “CodeIgniter Multiple Environment configuration”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s