Laravel 5.2 {{ env('APP_ENV') }} does not work in production
Asked Answered
S

3

10

The following code works on my development environment (Ubuntu 14.04 desktop)

@if(env('APP_ENV') === 'production')
    // ... something

But for some reason, the blade views on production (ubuntu 14.04 server) aren't getting the env variables.

If I run php artisan env the response is just fine: Current application environment: production

The database connection works fine. And the env() helper called from the controllers, also works.

What I tried so far:

php artisan clear:cache php artisan config:cache php artisan clear:cache php artisan view:clear

I also restarted apache multiple times, no results.

Any advice? Thanks!

Sprayberry answered 19/8, 2016 at 19:39 Comment(4)
what do you get if you {{ dd(env('APP_ENV')) }} in your template?Bays
@Bays null in production, "local" in local env.Sprayberry
did you forget to create your .env file? pretty sure laravel uses production by default but i don't know what you've changed.Bays
Nope, .env file exists and works fine with laravel except for views. The database connection works fine, and I CAN access env variables from controllers. The problem is with the views.Sprayberry
S
27

So, I made it work.

php artisan config:clear did the trick.

  1. php artisan config:clear
  2. php artisan cache:clear
  3. service apache2 restart (just in case)

What I guess the problem was:

The first time I run php artisan config:cache my .env file had APP_ENV=local. So I had to clear that config cache in order to get the new env vars values.

Sprayberry answered 19/8, 2016 at 20:25 Comment(0)
B
1

I solved this problem with:

rm bootstrap/cache/config.php
Beitz answered 31/3, 2021 at 17:25 Comment(0)
G
0

Instead of env() use \Config::get() or config().

https://laravel.com/docs/5.4/configuration#configuration-caching

What is difference between use env('APP_ENV'), config('app.env') or App::environment() to get app environment?

Gabbert answered 12/6, 2017 at 22:55 Comment(1)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.Chancellery

© 2022 - 2024 — McMap. All rights reserved.