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.
// EventServiceProvider.php
protected $listen = [
\App\Events\FooEvent::class => [
\App\Listeners\FooListener::class,
],
];
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.
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.
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.
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.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community