1

This is the env variable in my config/app.php

'env' => env('APP_ENV', 'production'),

I changed the APP_ENV variable to staging from .env like the following:

APP_ENV=staging

I run php artisan config:clear.

After that, php artisan env returned:

Current application environment: staging

However, the App::environment() still returns local.

Route::get('/', function () {
    dd(App::environment());
});

Any ideas? Did I miss something?

Autodesk
  • 631
  • 8
  • 27
  • Does this answer your question? [What is difference between use env('APP\_ENV'), config('app.env') or App::environment() to get app environment?](https://stackoverflow.com/questions/40026893/what-is-difference-between-use-envapp-env-configapp-env-or-appenviron) – Xupitan Jun 15 '22 at 04:25
  • Please add code from the `config/app.php` to the post, where the environment config is set. – Nikolai Kiselev Jun 15 '22 at 06:26
  • @NikolaiKiselev I add the content of my `config/app.php`, is there anything that I need to provide? Thank you! – Autodesk Jun 15 '22 at 07:42

1 Answers1

1

It seems, on the application level, the environment variable is set correctly. The value should be staging.

However, it can be overridden by your web server settings.

The current application environment detection can be overridden by defining a server-level APP_ENV environment variable.

Source: Laravel 9 Determining The Current Environment

You could check the web server settings.

E.g. with Apache server, the environment variable could be set, in files like httpd.conf as following:

SetEnv ENVIRONMENT "local"
Nikolai Kiselev
  • 6,201
  • 2
  • 27
  • 37
  • Thank you for these information. I use sail to develop laravel website, there is a similar setting `environment=LARAVEL_SAIL="1"` in `/etc/supervisor/conf.d/supervisord.conf`. Maybe it is not a good idea to modify the setting. So I think I need to find another way to to test if `App::environment()` works. – Autodesk Jun 16 '22 at 01:42
  • @Autodesk, Laravel Sail should definitely pick up changes in `.env`. Did you restart Sail after making the change? – Nikolai Kiselev Jun 16 '22 at 04:40
  • 1
    It works! Thank you so much! But I didn't restart Sail, because I use Sail with dev-container on vscode, so I decided to rebuild the container. – Autodesk Jun 16 '22 at 06:58