How to use .env file in codeigniter

Why .env?

You should never store sensitive credentials in your code. Storing configuration in the environment is one of the tenets of a twelve-factor app. Anything that is likely to change between deployment environments – such as database credentials or credentials for 3rd party services – should be extracted from the code into environment variables.

Basically, a .env file is an easy way to load custom configuration variables that your application needs without having to modify .htaccess files or Apache/nginx virtual hosts. This means you won't have to edit any files outside the project, and all the environment variables are always set no matter how you run your project - Apache, Nginx, CLI, and even PHP's built-in webserver. It's WAY easier than all the other ways you know of to set environment variables, and you're going to love it!

  • NO editing virtual hosts in Apache or Nginx
  • NO adding php_value flags to .htaccess files
  • EASY portability and sharing of required ENV values
  • COMPATIBLE with PHP's built-in web server and CLI runner

PHP dotenv is a PHP version of the original Ruby dotenv.

Installation

Installation is super-easy via Composer:

$ composer require vlucas/phpdotenv

or add it by hand to your composer.json file.

Note : We need to install this package in the root of of our project.

It will create vendor folder in the root of project. Now we need to load this package in our project and use it.

Step 1 : Create .env file

In order to use .env file first we need to create one in the root of the project and define some configuration in the file. Paste the code below in .env file for database connection 

DB_HOST=localhostDB_USER=rootDB_PASSWORD=harendra21@HKDB_NAME=ciblog

Step 2 : Load it in project

To load this configuration we need to include the `vendor/autoload.php` in our index.php and need to read `env` file

require_once './vendor/autoload.php';$dotenv = Dotenv\Dotenv::createImmutable('./');$dotenv->load();function env($name){	return $_ENV[$name];}

Note : In `$dotenv = Dotenv\Dotenv::createImmutable('./');` I am assuming the .env file is created at the root of the project. for more details you can refer to the link 

I defined a function env which takes name of configuration as parameter and return the value.

Step 3 : Start using

Now our configuration is avaliable globally so we can use it from any where in the project. I have define database configuration in .env file so I have to change `application/config/database.php`.

$db['default'] = array(	'dsn'	=> '',	'hostname' => env('DB_HOST'),	'username' => env('DB_USER'),	'password' => env('DB_PASSWORD'),	'database' => env('DB_NAME'),	'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' => array(),	'save_queries' => TRUE);

In the following database config I have cahnged hostname, username, database and password with .env file config. 

Thank You !

Categories