Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i add .env to codeigniter?

I try to make php connect to Outlook by follow this https://docs.microsoft.com/en-us/outlook/rest/php-tutorial but for environment i don't know where to put it or create new file. Please help and explain to me?

OAUTH_APP_ID=YOUR_APP_ID_HERE
OAUTH_APP_PASSWORD=YOUR_APP_PASSWORD_HERE
OAUTH_REDIRECT_URI=http://localhost:8000/authorize
OAUTH_SCOPES='openid profile offline_access User.Read Mail.Read'
OAUTH_AUTHORITY=https://login.microsoftonline.com/common
OAUTH_AUTHORIZE_ENDPOINT=/oauth2/v2.0/authorize
OAUTH_TOKEN_ENDPOINT=/oauth2/v2.0/token
like image 898
swett.bp Avatar asked Aug 24 '17 04:08

swett.bp


2 Answers

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.

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

This will load your env file in environment.

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

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

  1. Autoload library

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

$autoload['libraries'] = array('env');
  1. Autoload helper

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

$autoload['helper'] = array('general');
  1. Create an .env file Create an .env file in your project root folder.

  2. Access an env variable

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

env('MY_VARIABLE');

Source: https://github.com/technoknol/env-in-CodeIgniter

like image 114
shyammakwana.me Avatar answered Oct 21 '22 18:10

shyammakwana.me


I suggest you create a new file named .env, and put it on you web root. Then, install package vlucas/phpdotenv use composer . This package can Loads environment variables from .env to getenv(), $_ENV and $_SERVER automagically. The Laravel framework distinguish environment by it.

like image 20
yankewei Avatar answered Oct 21 '22 18:10

yankewei