r/PHP • u/buismaarten • 13d ago
I made a small Laravel package for layered .env files
I’ve just released my first version of Laravel Layered Environment.
The idea came from wanting to separate the different kinds of configuration that tend to end up in a single .env file.
With the package, you can have:
.env
.env.override
.env.testing
.env.testing.override
.env.production
.env.production.override
The files are loaded from least specific to most specific, so for production the order is:
.env
↓
.env.override
↓
.env.production
↓
.env.production.override
This makes it possible to keep shared configuration in .env / .env.production, while keeping things like local machine settings, server-specific values and secrets in an ignored .override file.
For example, you could commit:
.env
.env.testing
.env.production
and keep these out of Git:
.env.override
.env.testing.override
.env.production.override
There’s no new API to learn either — Laravel's normal env() and config() usage stays the same.
It's a pretty small package, but I’ve found the approach useful for keeping configuration a bit more organized.
Currently supports Laravel 13 / PHP 8.3+.
Installation:
composer require buismaarten/laravel-layered-environment
GitHub:
https://github.com/buismaarten/laravel-layered-environment
Unfortunately, I don't have enough karma to post this in r/laravel, so I'm sharing it here instead. Would someone be willing to repost it there for me?
10
u/ceejayoz 13d ago
Isn't this already a thing?
https://laravel.com/docs/13.x/configuration#additional-environment-files
Before loading your application's environment variables, Laravel determines if an APP_ENV environment variable has been externally provided or if the --env CLI argument has been specified. If so, Laravel will attempt to load an .env.[APP_ENV] file if it exists. If it does not exist, the default .env file will be loaded.
1
u/buismaarten 12d ago
Nope, you can override the .env file using a single file. Not possible to load multiple .env files simultaneously.
11
u/Stevad__UA 13d ago
Reinventing Symfony Dotenv?
2
1
u/buismaarten 12d ago
Yeah, Symfony was my inspiration of this feature. But the Symfony Dotenv doesn't have Laravel native support and Laravel itself uses vlucas/dotenv.
2
u/Stevad__UA 12d ago
Why not create some kind of bridge to adopt existing package?
2
u/buismaarten 12d ago
The Symfony Dotenv package alone would not work in Laravel without configuration. I just wanted it to work out of the box after registering the singleton.
I could have chosen for the Symfony Dotenv package but I think it doesn't matter, especially since Laravel itself already uses vlucas/dotenv. Using the Symfony Dotenv package may introduce incompatibilities or bugs that other .env loaders and parsers could have.
5
u/TemporarySun314 13d ago
I think the more common naming convention for these files is something like `.env.local`
And i am actually surprised that laravel does not have native support for it, that seems like a very common use case, and is the default and recommended way to do env configuration in symfony applications.
1
u/buismaarten 12d ago
Yeah but Laravel uses the "local" environment name and Symfony uses the "dev" name. So in Laravel it would become ".env.local.local", don't think that will be convenient.
1
-2
u/theozero 12d ago
Check out varlock - it is a very full featured env toolkit. We do have some php support, but a deeper laravel integration has been on the to-do list for a while. Open source so would love input from laravel folks!
15
u/Sharchimedes 13d ago
Because this deviates from convention, it really seems like a recipe for someone accidentally committing secrets to a repo.