Sometimes can happen that we need to have an environment variable to send it to some of our dependencies but in some environments maybe this is not set and we expect to be like that.

If we inject the value like this:

services:
  App\Infrastructure\SomeService:
    arguments:
      $logger: '@logger'
      $someVar: '%env(VAR_1)%'

And VAR_1 does not exist, we’ll get an error like this one:

Environment variable not found: 'VAR_1'.

The first option we have is to declare the env variable like this:

parameters:
  env(VAR_1): ''

Another option is to set a default parameter:

parameters:
  default_var_1: some_value
  var_1: '%env(string:default:default_var_1:VAR_1)%'

Resources: