Code Monkey home page Code Monkey logo

env-in-codeigniter's Introduction

Adding .env file (like laravel) support in CodeIgniter

StyleCI

This is a showcase how you can implement Laravel like .env file structure in your CodeIgniter application, and make configurations easy for all the development, production, testing stages. You don't need to take care of application/config/database.php file each time you do git pull or put it in .gitignore.

Steps

1. First add a package vlucas/phpdotenv via composer

Run composer require vlucas/phpdotenv command in your project root directory. (If you don't have composer.json, don't worry, composer will take care of it.

2. Copy a file Env.php to your application/libraries directory.

This will load your env file in environment.

If you are using vulcas/phpdotenv >= 4.x.x, then change code accordingly as mentioned in Env.php file.

3. Copy general_helper.php to your application/helpers directory.

This will add env helper method to get any variable stored in .env file.

4. Autoload library

Add library to $autoload['libraries'] like this

$autoload['libraries'] = array('env');
5. Autoload helper (application/config/autoload.php)

Add helper to $autoload['helper'] like this

$autoload['helper'] = array('general');
6. Autoload helper (application/config/autoload.php)

Make composer_autoload to vendor/autoload.php file path like this in config.php file

$config['composer_autoload'] = FCPATH. 'vendor'. DIRECTORY_SEPARATOR . 'autoload.php';
7. Create an .env file

Create an .env file in your project root folder.

8. Access an env variable

In your php code you can access any .env variable like below

env('MY_VARIABLE');

Voila ! Now you can have multiple variables based on Environment, and don't need to manually change file and then put it in .gitignore.

env-in-codeigniter's People

Contributors

technoknol avatar alielmajdaoui avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.