Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a default environment variable in Symfony

Tags:

symfony

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?

like image 563
Hammerbot Avatar asked May 14 '18 08:05

Hammerbot


Video Answer


1 Answers

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
like image 171
DrKey Avatar answered Oct 11 '22 10:10

DrKey