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:
- run
php artisan serve --env=testing
with a.env.testing
file withAPP_ENV=testing
- put a
{{ env("APP_ENV") }}
in any view or anything - Instead of
testing
, seelocal
It seems the environment is correctly loaded for artisan commands, but not for the resulting server process. Contrast the above with:
- run
php artisan tinker --env=testing
with a.env.testing
file withAPP_ENV=testing
- run
env("APP_ENV")
- result is as one would expect:
testing
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 29 (14 by maintainers)
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.
$_ENV
and$_SERVER
and callsputenv()
.$_ENV
and$_SERVER
.Adding
getenv()
andputenv()
to Laravel 5.8 fixes the issue: https://github.com/staudenmeir/framework/commit/2e1d6a95631aec49510e1b610074e9c25d7842b5All 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
.Thanks. I have done this.
I do believe this to be a bug. Steps to reproduce based solely on
laravel/laravel
:composer create-project --prefer-dist laravel/laravel test
.env
to.env.testing
.env.testing
APP_ENV
totesting
<h1> {{ env('APP_ENV') }} </h1>
to welcome.blade.phpphp artisan serve --env=testing
The page will show
local
, not the expectedtesting
. From within tinker however the correcttesting
will be shown.If still unconvinced this is a bug I’ll go to one of the support channels.