Codeigniter env

Codeigniter Codacy Badge StyleCI

Simple env setup for your codeigniter application with single or multiple .env configuration. Configure your application based on your need.

Load environment variables from .env for your application using getenv() or $_ENV.

Installation

  1. Install Composer
$ curl -s http://getcomposer.org/installer | php
  1. copy the .env.example and .htaccess from method1 or method2 in repo and paste it to your CodeIgniter root.
  2. Install package on root directory
$ composer require symfony/dotenv

Configuration

  1. Enable your Composer Autoload and Hooks: application/config/config.php

$config['enable_hooks'] = FALSE; to $config['enable_hooks'] = TRUE;

$config['composer_autoload'] = FALSE; to $config['composer_autoload'] = 'vendor/autoload.php';

  1. Add this code to your application hooks: application/config/hooks.php

Method 1 (Single env)

$hook['pre_system'] = function () {
    $dotenv = new Symfony\Component\Dotenv\Dotenv();
    $dotenv->load(__DIR__.'/../../.env');
};

Method 2 (Multiple env)

$hook['pre_system'] = function () {
    $dotenv = new Symfony\Component\Dotenv\Dotenv();
    $dotenv->load(__DIR__.'/../../.env.'.getenv('CI_ENV'));
};
  1. Create your .env files

Using Method 1(Single env)

$ cp .env.example .env

Define your application environment CI_ENV variable in .env only for this method.

Using Method 2 (Multiple env)

Predefined Environments (development, testing, production)

$ cp .env.example .env.development

Use these environment names only. For additional environments add a switch case in index.php Line No. : 66 Set the environment in .htaccess file. Line No. : 3 & 12. If environment is not set in .htaccess file the application won't run.

<IfModule mod_env.c>
    #Set Environment(development,testing,production)
    SetEnv CI_ENV development
</IfModule>
<IfModule mod_rewrite.c>
    #if mod_env module not enabled in server
    #Set Environment(development,testing,production)
    RewriteRule ^ - [E=CI_ENV:development]
</IfModule>

Usage Example

Database Configuration

  1. Edit the application/config/database.php
  2. Replace this code:
$db['default'] = [
	'dsn'	=> '',
	'hostname' => 'localhost',
	'username' => '',
	'password' => '',
	'database' => '',
	'dbdriver' => 'mysqli',
	'dbprefix' => '',
	'pconnect' => FALSE,
	'db_debug' => (ENVIRONMENT !== 'production'),
	'cache_on' => FALSE,
	'cachedir' => '',
	'char_set' => 'utf8',
	'dbcollat' => 'utf8_general_ci',
	'swap_pre' => '',
	'encrypt' => FALSE,
	'compress' => FALSE,
	'stricton' => FALSE,
	'failover' => [],
	'save_queries' => TRUE

];

to

$db['default'] = [
    'dsn'          => '',
    'hostname'     => getenv('DB_HOSTNAME'),
    'username'     => getenv('DB_USERNAME'),
    'password'     => getenv('DB_PASSWORD'),
    'database'     => getenv('DB_DATABASE'),
    'dbdriver'     => 'mysqli',
    'dbprefix'     => getenv('DB_PREFIX'),
    'pconnect'     => false,
    'db_debug'     => (ENVIRONMENT !== 'production'),
    'cache_on'     => false,
    'cachedir'     => '',
    'char_set'     => getenv('DB_CHAT_SET'),
    'dbcollat'     => getenv('DB_COLLATION'),
    'swap_pre'     => '',
    'encrypt'      => false,
    'compress'     => false,
    'stricton'     => false,
    'failover'     => [],
    'save_queries' => true,

];