Laravel framework 5.0 article

Home >> Laravel framework 5.0 >>

  Setup multiple environment for laravel 5

This article will explain you, how to setup Multiple Environment for Laravel 5 

When you install laravel 5 one .env file will be created during installation.

In this article I will setup two environment, local & production, check the below steps

1. Create two Environment files name as local & production.

  • Create a file with the name of .local.env
  • Create a file with the name of .production.env

2. Copy the .env file content and paste in .local.env and use APP_ENV=local because this file will be used for local Environment, which will look like

APP_ENV=local
APP_DEBUG=true
APP_KEY=k0VP46ToSeg3pF8MHE4ZBNhZXc0uTr19

DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

CACHE_DRIVER=file
QUEUE_DRIVER=sync

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null

3. Copy the .env file content and paste in .production.env and use APP_ENV=production because this file will be used for production Environment, which will loook like

APP_ENV=production
APP_DEBUG=true
APP_KEY=k0VP46ToSeg3pF8MHE4ZBNhZXc0uTr19

DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

CACHE_DRIVER=file
QUEUE_DRIVER=sync

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null

4. Remove all content from .env file. Use local for local Environment and production for production Environment

.env file for local Environment look like

local

.env file for production Environment look like

production

5. Create new php file and named it, environment.php, save it into this folder: app/bootstrap/environment.php and then paste the below code in this file.

<?php
/*
|--------------------------------------------------------------------------
| Detect The Application Environment
|--------------------------------------------------------------------------
*/

$env = $app->detectEnvironment(function(){
$envPath = trim(__DIR__.'/../.env');
$setEnv = trim(file_get_contents($envPath));
 if (file_exists($envPath))
 {
  putenv("APP_ENV=$setEnv");
  if (getenv('APP_ENV') && file_exists(__DIR__.'/../.' .getenv('APP_ENV') .'.env')) {
   Dotenv::load(__DIR__ . '/../', '.' . getenv('APP_ENV') . '.env');
  } 
 }
});

?>

6. Include your environment.php file in bootstrap file. Paste it inside your bootstrap/app.php file.

<?php

/*
|--------------------------------------------------------------------------
| Load Environment File on Startup
|--------------------------------------------------------------------------
|
| This will determine, which environment will be loaded for our application.
|
*/
require __DIR__.'/environment.php';

?>

7. After that you can check your environment using your terminal

// Write local in .env file and then check the environment using below artisan command.
php artisan env
Current application environment: local

Change local as production and then use the below command from your terminal

//  Write production in .env file and then check the environment using below artisan command
php artisan env
Current application environment: production

Comments

well done

Thanks for the answers! it was really helpful...

  2 years ago      Rohit      
nice explanation

It's really nice explanation

  2 years ago      John Smith      
For setting up multiple environment for laravel 5.4

Hi please do let me know about the laravel 5.4 i need to do same for this. I have try your solutions but its not working for me on 5.4. Please do update it.

  10 months ago      JM      
Doesnt Work

undefined variable: $env error!!

  6 months ago      Bob      
Use this:
Re : Doesnt Work.

1 change line "$env = $app->detectEnvironment(function(){" to "$envir Illuminate\Foundation\EnvironmentDetector;
$env = $environmentDetector->detect(function() {"
2) change line "Dotenv::load(__DIR__ . '/../', '.' . getenv('APP_ENV') . '.env');" to "$dotenv = new Dotenv\Dotenv(__DIR__);
if(file_exists(".env")) {
$dotenv->load();
}"

  4 months ago      Penguin      
bad copypaste
Re : Use this:.

1 "$envir Illuminate\Foundation\EnvironmentDetector;
$env = $environmentDetector->detect(function() {"
instead of "$env = $app->detectEnvironment(function(){"
2) " $dotenv = new Dotenv\Dotenv(__DIR__);
if(file_exists(".env")) {
$dotenv->load();
}"
instead of "Dotenv::load(__DIR__ . '/../', '.' . getenv('APP_ENV') . '.env');"

  4 months ago      Penguin      
This fucking site cut my string!
Re : bad copypaste.

in first line create new Illuminate\Foundation\EnvironmentDetector:
$envir Illuminate\Foundation\EnvironmentDetector;

  4 months ago      Penguin      
ADMIN, FIX THIS STUPID BUG!!!
Re : This fucking site cut my string!.

when i write
$envir Illuminate\Foundation\EnvironmentDetector;
it CUT my text!

  4 months ago      Penguin      
AGAIN!!! WTF!!!
Re : ADMIN, FIX THIS STUPID BUG!!!.

It must be
variable called "environmentDetector"
equals "new Illuminate\Foundation\EnvironmentDetector"
BUT I CANT WRITE THIS BECOUSE OF BUG IN THIS SITE!
WTF!!!

  4 months ago      Penguin      

Write your comment now