1

I understand with Laravel 5, it uses .env files so we can set specific enivornment values.

My question is, is there a way in Laravel 5, to say for example,

if ($SERVER_NAME == "my_production_server") {
    $environment = "production"
}

And from that it uses production values. My thinking is, I'd like all the environments and their variables placed in one file or directory, or whatever so we can deploy the whole build without any manual intervention, and we can check this all into our code repository.

miken32
  • 42,008
  • 16
  • 111
  • 154
KingKongFrog
  • 13,946
  • 21
  • 75
  • 124
  • For those who just want to find the current environment value simple use `if (App::environment('local'))` https://laravel.com/docs/5.7/configuration#determining-the-current-environment – Adam Dec 02 '18 at 13:00

3 Answers3

4

Laravel 5 has made this a little harder than before but here's the way to do it. All you will need to do after this is change a value of your .env file and the environment will change

The steps to do this are as follows

  1. Look at your local .env installed by Laravel and change its contents to local or production or whatever else you need

  2. Create 2 files .local.env and .production.env

  3. Add default environment value:

    • In .local.env : APP_ENV=local
    • In .production.env : APP_ENV=production
  4. Create new php file and named it, environment.php, save it into this folder: app/bootstrap/environment.php

    $env = $app->detectEnvironment(function(){
      $environmentPath = __DIR__.'/../.env';
       $setEnv = trim(file_get_contents($environmentPath));
       if (file_exists($environmentPath)){
              putenv("APP_ENV=$setEnv");
              if (getenv('APP_ENV') && file_exists(__DIR__.'/../.' .getenv('APP_ENV') .'.env')) {
                   Dotenv::load(__DIR__ . '/../', '.' . getenv('APP_ENV') . '.env');
              } 
       }
    });
    
  5. Include your environment.php file in bootstrap file. Paste it inside your bootstrap/app.php file.

    require __DIR__.'/environment.php';
    

Yay! You're done.

NOTE: If Laravel can't find a .env file it automatically uses .production.env which makes it awesome for deployments

Credit to http://developers.ph/laravel-framework/laravel-5/how-to-setup-multiple-environment-for-laravel-5-developers-way/

cjds
  • 8,268
  • 10
  • 49
  • 84
  • This is great Carl. My only question is, how does it differentiate between local and production? – KingKongFrog Jun 26 '15 at 21:50
  • :-) Thought that might be confusing. You specify it in your `.env` file. Just add the word `local ` and it will be local otherwise production or whatever else – cjds Jun 26 '15 at 21:52
  • Nevermind, I see that you are manually setting it in the .env file. Now is there a way to dynamically set that. Your first part was very helpful....this is where I'm trying to see if its possible as well. – KingKongFrog Jun 26 '15 at 21:53
  • Actually I can probably code this in the enivornment.php file. For example, if IP address is 1.2.3.4 use local etc. – KingKongFrog Jun 26 '15 at 21:54
  • Yep. That's the way. You can use the `environment.php` replace `getenv('APP_ENV')` with the variable you set from the IP or whatever you use to tell production from local – cjds Jun 26 '15 at 21:55
  • @cjds I did all you adviced above but `var_dump(getenv("APP_ENV"))` still returns `false`. What can be wrong else here? – Alliswell Aug 01 '15 at 19:25
0

You could probably set the .env as:

APP_ENV=local
APP_DEBUG=true
APP_KEY=sjkanljksdnjsnetcetcetcetc..
APP_URL=http://localhost:8000

DB_HOST=qa
DB_DATABASE=admin
DB_USERNAME=home
DB_PASSWORD=root

DB_HOST2=production// I.P address
DB_DATABASE2=admin
DB_USERNAME2=admin
DB_PASSWORD2=admin

Then in the database.php file in the config folder, you could set up the connections Host and Host2 to match the localhost and production values.

Sajal
  • 4,359
  • 1
  • 19
  • 39
0

I have it like this For Laravel 5.0. I followed cjds guide but changed the code my specification. It requires no .env files.

$env = $app->detectEnvironment(function() {
    if (php_sapi_name() === 'cli') {
        if (strpos(getcwd(), 'production') !== false) {
            return 'production';
        } elseif (strpos(getcwd(), 'staging') !== false) {
            return 'staging';
        } elseif ((strpos(getcwd(), 'xampp') !== false) || (strpos(getcwd(), 'lampp') !== false)) {
            return 'development';
        } else {
            return 'production';
        }
    }

    $absoluteLink = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    if (strpos($absoluteLink, 'production') !== false) {
        return 'production';
    } elseif (strpos($absoluteLink, 'staging') !== false) {
        return 'staging';
    } elseif (strpos($absoluteLink, '.dev') !== false || strpos($absoluteLink, 'local') !== false || strpos($absoluteLink, '192.168') !== false) {
        return 'development';
    } else {
        return 'production';
    }
});

putenv("APP_ENV=" . $env);
Raza
  • 3,147
  • 2
  • 31
  • 35