Support the ongoing development of Laravel.io →
posted 3 years ago
Laravel
  • Laravel Version: 8.54.0
  • PHP Version: 7.4.14
  • Database Driver & Version: Mysql / 8.0.23

The Problem

A composer package my app requires uses $_ENV to access env vars. I'm using a class from this package in a listener that is registered in EventServiceProvider in the $listen property. The $_ENV values are not set. The $_ENV variables are correctly loaded from .env everywhere else. This package is not Laravel specific, so env() helper is not an option.

This issue, which is closed, mentions the same issue but, I thought getenv is discouraged and should be avoided being it is not thread safe.

Steps To Reproduce:

  • Register a listener in EventServiceProvider@$listen

// EventServiceProvider.php
protected $listen = [
        \App\Events\FooEvent::class    => [
            \App\Listeners\FooListener::class,
        ],
];
  • In FooListener::class, call $_ENV['FOO'];
Last updated 3 years ago.
0

I think you'll have to take this up with the package maintainer to have them change that behavior.

If you're using queue:listen like in that linked GitHub issue, the problem may be that the queue:listen command creates sub-processes (separate queue:work commands) that don't seem to inherit the parent's environment variables.

0

@fideloper

Thanks for your reply!

I am not the maintainer of the package, but a contributor writing a PR. And this isn't a problem with the package. The package is just vanilla php. It's definitely an issue with Laravel. But I will look into what you suggested. Thanks.

0

Well, I can't find anywhere in Laravel empties the $_ENV global, but there are definitely circumstances around the DotEnv package + how processes work that might be making $_ENV empty in some cases.

Perhaps your AppServiceProvider can set $_ENV['foo'] = 'bar'; with the appropriate key/variable if it finds that variable it's not already set.

ctwillie liked this reply

1

@fideloper

Thanks for commenting again!

And yea, I'll have to find some other option, like binding to the container in a service provider like you have suggested.

But there is a ton of issues out there referencing this behavior with $_ENV.

One here.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.