I am using Symfony 3.4 and I am trying to set a default value for an environment variable:
I have a .env
file at the root of my project containing:
DB_PASSWORD=someValue
and in my app/config/parameters.yml
, I have the following:
parameters:
...
database_password: '%env(DB_PASSWORD)%'
...
I would like to make the definition of the DB_PASSWORD
variable optionnal in the .env
file, but I can't find any documentation about how to do such a thing.
I tried to do the following:
parameters:
...
database_password: '%env(DB_PASSWORD, "default")%'
...
without success.
How can I define a default value for an environment variable in Symfony?
You just have to declare env(DB_PASSWORD): default_value
in your parameters.yml
as stated in the documentation
You can also give the env() parameters a default value: the default value will be used whenever the corresponding environment variable is not found:
# app/config/parameters.yml
parameters:
database_host: '%env(DATABASE_HOST)%'
env(DATABASE_HOST): localhost
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With