framework: Serve command loads server in wrong environment

  • Laravel Version: 5.8.3
  • PHP Version: 7.2.15
  • Database Driver & Version: sqlite locally (irrelevant I believe)

Description:

When using the artisan serve command the correct environment is not loaded.

Steps To Reproduce:

  1. run php artisan serve --env=testing with a .env.testing file with APP_ENV=testing
  2. put a {{ env("APP_ENV") }} in any view or anything
  3. Instead of testing, see local

It seems the environment is correctly loaded for artisan commands, but not for the resulting server process. Contrast the above with:

  1. run php artisan tinker --env=testing with a .env.testing file with APP_ENV=testing
  2. run env("APP_ENV")
  3. result is as one would expect: testing

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 29 (14 by maintainers)

Most upvoted comments

That’s very odd. I just tested again and I can indeed reproduce it. Not sure why it didn’t the first time I tried.

Try resetting the configuration cache: php artisan config:clear

It is now Augustus 2022 and this is still a issue! This hasnt’t been fixed yet!! Does anybody have a solution?

This is caused by the different ways Laravel 5.7 and 5.8 set environment variables.

  • 5.7 sets the variables in $_ENV and $_SERVER and calls putenv().
  • 5.8 only sets the variables in $_ENV and $_SERVER.

Adding getenv() and putenv() to Laravel 5.8 fixes the issue: https://github.com/staudenmeir/framework/commit/2e1d6a95631aec49510e1b610074e9c25d7842b5

All the tests still work. Can you approve this @GrahamCampbell?

isnt the env helper meant to only be used in config files?

(not sure if actually related or not)

I really can’t see what changed it. Nothing really has changed to the ServeCommand.

Try resetting the configuration cache: php artisan config:clear

Thanks. I have done this.

I do believe this to be a bug. Steps to reproduce based solely on laravel/laravel:

  1. composer create-project --prefer-dist laravel/laravel test
  2. copy .env to .env.testing
  3. change in .env.testing APP_ENV to testing
  4. add <h1> {{ env('APP_ENV') }} </h1> to welcome.blade.php
  5. run php artisan serve --env=testing

The page will show local, not the expected testing. From within tinker however the correct testing will be shown.

If still unconvinced this is a bug I’ll go to one of the support channels.