Environment Variable Processors
Using env vars to configure Symfony applications is a common practice to make your applications truly dynamic.
The main issue of env vars is that their values can only be strings and your
application may need other data types (integer, boolean, etc.). Symfony solves
this problem with “env var processors”, which transform the original contents of
the given environment variables. The following example uses the integer
processor to turn the value of the HTTP_PORT env var into an integer:
Built-In Environment Variable Processors
Symfony provides the following env var processors:
env(string:FOO)Casts
FOOto a string:env(bool:FOO)Casts
FOOto a bool (truevalues are'true','on','yes'and all numbers except0and0.0; everything else isfalse):env(not:FOO)Casts
FOOto a bool (just asenv(bool:...)does) except it returns the inverted value (falsy values are returned astrue, truthy values are returned asfalse):env(int:FOO)Casts
FOOto an int.env(float:FOO)Casts
FOOto a float.env(const:FOO)Finds the const value named in
FOO:env(base64:FOO)Decodes the content of
FOO, which is a base64 encoded string.env(json:FOO)Decodes the content of
FOO, which is a JSON encoded string. It returns either an array ornull:env(resolve:FOO)If the content of
FOOincludes container parameters (with the syntax%parameter_name%), it replaces the parameters by their values:env(csv:FOO)Decodes the content of
FOO, which is a CSV-encoded string:env(shuffle:FOO)Randomly shuffles values of the
FOOenv var, which must be an array.New in version 6.2: The
env(shuffle:...)env var processor was introduced in Symfony 6.2.env(file:FOO)Returns the contents of a file whose path is the value of the
FOOenv var:env(require:FOO)require()the PHP file whose path is the value of theFOOenv var and return the value returned from it.env(trim:FOO)Trims the content of
FOOenv var, removing whitespaces from the beginning and end of the string. This is especially useful in combination with thefileprocessor, as it’ll remove newlines at the end of a file.env(key:FOO:BAR)Retrieves the value associated with the key
FOOfrom the array whose contents are stored in theBARenv var:env(default:fallback_param:BAR)Retrieves the value of the parameter
fallback_paramwhen theBARenv var is not available:When the fallback parameter is omitted (e.g.
env(default::API_KEY)), then the returned value isnull.env(url:FOO)Parses an absolute URL and returns its components as an associative array.
# .env MONGODB_URL="mongodb://db_user:db_password@127.0.0.1:27017/db_name"
Caution
In order to ease extraction of the resource from the URL, the leading
/is trimmed from thepathcomponent.env(query_string:FOO)Parses the query string part of the given URL and returns its components as an associative array.
# .env MONGODB_URL="mongodb://db_user:db_password@127.0.0.1:27017/db_name?timeout=3000"
env(enum:FooEnum:BAR)Tries to convert an environment variable to an actual
\BackedEnumvalue. This processor takes the fully qualified name of the\BackedEnumas an argument.# App\Enum\Environment enum Environment: string { case Development = 'dev'; case Production = 'prod'; }
New in version 6.2: The
env(enum:...)env var processor was introduced in Symfony 6.2.
It is also possible to combine any number of processors:
Custom Environment Variable Processors
It’s also possible to add your own processors for environment variables. First,
create a class that implements
Symfony\Component\DependencyInjection\EnvVarProcessorInterface:
use Symfony\Component\DependencyInjection\EnvVarProcessorInterface;
class LowercasingEnvVarProcessor implements EnvVarProcessorInterface
{
public function getEnv(string $prefix, string $name, \Closure $getEnv)
{
$env = $getEnv($name);
return strtolower($env);
}
public static function getProvidedTypes()
{
return [
'lowercase' => 'string',
];
}
}
To enable the new processor in the app, register it as a service and
tag it with the container.env_var_processor
tag. If you’re using the
default services.yaml configuration,
this is already done for you, thanks to autoconfiguration.