Backward compatible support for Laravel's pre-5.8 env() function.

v1.5.0 2021-12-25 07:28 UTC

This package is auto-updated.

Last update: 2022-01-25 07:44:24 UTC


TravisCI Test Coverage


  • Works with any package, even non-Laravel ones (it was my secondary reason)
  • You no longer have to fear Laravel just going one day "Oops! We're not going to read from the environment anymore! Too bad!" on a non-major release.


With the introduction of Laravel 5.8, they broke the essential env() function by changing it from reading from/writing to environment variables to only using the global $_SERVER array, which is totally useless in console-based apps.


Therefore, I took it upon myself to create this polyfill to ensure that the pre-5.8 behaviors remain intact.

Additionally, this is a great little utility function to use outside of Laravel-specic projects that also want to use the env() function.

It is specially configured with a 0.0.1 composer vendor and a AAutoloadFirst namespace, so that composer will always load it before it gets to Laravel. See

The source code is lifted directly from the last version of Laravel 5.7.

As you can imagine, most of the copyright in this project belongs to Taylor Otwell, 2018.

It also includes 100% unit test code .


Via Composer

composer require phpexperts/laravel-env-polyfill


$foo = \env('foo'); // 'bar'


if (in_array(SomeTrait::class, class_uses_recursive($this)) {
    // The class or its child it's being called from uses SomeTrait.

Use cases

✔ Env
✔ Env with quotes
✔ Env true
✔ Env false
✔ Env empty
✔ Env null
✔ Value
✔ Env will use default value if needed




Theodore R. Smith
GPG Fingerprint: 4BF8 2613 1C34 87AC D28F 2AD8 EB24 A91D D612 5690
CEO: PHP Experts, Inc.

Taylor Otwell


MIT license. Please see the license file for more information.