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 FOO to a string:

env(bool:FOO)

Casts FOO to a bool (true values are 'true', 'on', 'yes' and all numbers except 0 and 0.0; everything else is false):

env(not:FOO)

Casts FOO to a bool (just as env(bool:...) does) except it returns the inverted value (falsy values are returned as true, truthy values are returned as false):

env(int:FOO)

Casts FOO to an int.

env(float:FOO)

Casts FOO to 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 or null:

env(resolve:FOO)

If the content of FOO includes 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 FOO env 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 FOO env var:

env(require:FOO)

require() the PHP file whose path is the value of the FOO env var and return the value returned from it.

env(trim:FOO)

Trims the content of FOO env var, removing whitespaces from the beginning and end of the string. This is especially useful in combination with the file processor, as it’ll remove newlines at the end of a file.

env(key:FOO:BAR)

Retrieves the value associated with the key FOO from the array whose contents are stored in the BAR env var:

env(default:fallback_param:BAR)

Retrieves the value of the parameter fallback_param when the BAR env var is not available:

When the fallback parameter is omitted (e.g. env(default::API_KEY)), then the returned value is null.

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 the path component.

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 \BackedEnum value. This processor takes the fully qualified name of the \BackedEnum as 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.