Reputation: 6166
Does Symfony 4 somehow save or cache the environment variable parameters in the "prod" environment or does it retrieve them on every request?
My problem is that when I host my Symfony application using Apache, I need to list all the environment variables by defining them all in the VirtualHost
configuration using SetEnv
directives; and I would prefer not having to do that.
I would expect that when using the PHP CLI, I could "compile" the Symfony project by capturing or caching the parameters so that when the application is run through Apache, it uses these "cached" parameters.
Is it doable or the SetEnv
approach is the only one?
Upvotes: 2
Views: 2850
Reputation: 47308
Symfony uses the DotEnv component to deal with environment variables.
The DotEnv component reads .env
files and load its contents on environment variables.
.env
file, this one is loaded first. If there is no .env
, file, but a .env.dist
file exists, this one is used in its place . .env.local
file exists, it is loaded now. This file should never be committed to a source code repository, and its values override those in the previous files.env.$env.local
exists (or if it doesn't but .env.$env
exist), is loaded now. Again, these values override those that came before.This will populate all the declared environment variables, but it won't override existing environment variables. There is performance hit from reading .env
files, but hit is practically negligible.
That these are loaded depend on the bootstrap.php
file, that is loaded by default on public/index.php
and bin/console
; so the approach serves both HTTP request and console applications, so you could use to store these values instead of using SetEnv
directives in your Apache configuration.
But wait, there is more...
On top of the above, you can dump all these variables into a PHP-optimised file so that the .env
files need not to be parsed on each request.
To do that, you simply execute:
composer dump-env prod
This will create a .env.local.php
file. And if this file exists, none of the above will be read at all.
Upvotes: 2