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
  • code
  • source
  1. APP_ENV=local
  2. APP_DEBUG=true
  3. APP_KEY=k0VP46ToSeg3pF8MHE4ZBNhZXc0uTr19
  4. DB_HOST=localhost
  5. DB_DATABASE=homestead
  6. DB_USERNAME=homestead
  7. DB_PASSWORD=secret
  8. CACHE_DRIVER=file
  9. QUEUE_DRIVER=sync
  10. MAIL_DRIVER=smtp
  11. MAIL_HOST=mailtrap.io
  12. MAIL_PORT=2525
  13. MAIL_USERNAME=null
  14. 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
  • code
  • source
  1. APP_ENV=production
  2. APP_DEBUG=true
  3. APP_KEY=k0VP46ToSeg3pF8MHE4ZBNhZXc0uTr19
  4. DB_HOST=localhost
  5. DB_DATABASE=homestead
  6. DB_USERNAME=homestead
  7. DB_PASSWORD=secret
  8. CACHE_DRIVER=file
  9. QUEUE_DRIVER=sync
  10. MAIL_DRIVER=smtp
  11. MAIL_HOST=mailtrap.io
  12. MAIL_PORT=2525
  13. MAIL_USERNAME=null
  14. 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
  • code
  • source
  1. local
.env file for production Environment look like
  • code
  • source
  1. 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.
  • code
  • source
  1. <?php
  2. /*
  3. |--------------------------------------------------------------------------
  4. | Detect The Application Environment
  5. |--------------------------------------------------------------------------
  6. */
  7. $env = $app->detectEnvironment(function(){
  8. $envPath = trim(__DIR__.'/../.env');
  9. $setEnv = trim(file_get_contents($envPath));
  10. if (file_exists($envPath))
  11. {
  12. putenv("APP_ENV=$setEnv");
  13. if (getenv('APP_ENV') && file_exists(__DIR__.'/../.' .getenv('APP_ENV') .'.env')) {
  14. Dotenv::load(__DIR__ . '/../', '.' . getenv('APP_ENV') . '.env');
  15. }
  16. }
  17. });
  18. ?>
6. Include your environment.php file in bootstrap file. Paste it inside your bootstrap/app.php file.
  • code
  • source
  1. <?php
  2. /*
  3. |--------------------------------------------------------------------------
  4. | Load Environment File on Startup
  5. |--------------------------------------------------------------------------
  6. |
  7. | This will determine, which environment will be loaded for our application.
  8. |
  9. */
  10. require __DIR__.'/environment.php';
  11. ?>
7. After that you can check your environment using your terminal
  • code
  • source
  1. // Write local in .env file and then check the environment using below artisan command.
  2. php artisan env
  3. Current application environment: local
Change local as production and then use the below command from your terminal
  • code
  • source
  1. // Write production in .env file and then check the environment using below artisan command
  2. php artisan env
  3. Current application environment: production

Comments

Popular Posts