Cover of the book Symfony 5: The Fast Track

Symfony 5: The Fast Track is the best book to learn modern Symfony development, from zero to production. +300 pages showcasing Symfony with Docker, APIs, queues & async tasks, Webpack, SPAs, etc.

New in Symfony 4.3: Default and Trim Env Var Processors

Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.

Env Var processors allow to transform the value of environment variables before using them in the Symfony app configuration. They are useful for example to transform the type of the env var (which, by definition is always a string) into a more suitable type, such as an integer or a boolean. In Symfony 4.3 we added new processors to provide new transformations.

Default Env Var Processor

Contributed by
Jérémy Derussé
in #28976.

It returns a default value when the given env var is not defined. It uses this syntax: env(default:DEFAULT_VALUE:ENV_VAR_NAME). You can combine it with any of the other env vars to do advanced things like the following:

1
2
3
4
# config/services.yaml
parameters:
    private_key: '%env(default:default_key:file:PRIVATE_KEY)%'
    default_key: '%env(PRIVATE_KEY)%'

Trim Env Var Processor

Contributed by
Maxime Steinhausser
in #29781.

It applies the trim() PHP function to the value of the env var before returning it. It's especially useful in combination with the file processor, as it'll remove newlines at the end of a file:

1
2
3
# config/services.yaml
parameters:
    private_key: '%env(trim:file:PRIVATE_KEY)%'
Help the Symfony project!

As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.

New in Symfony 4.3: Default and Trim Env Var Processors symfony.com/blog/new-in-symfony-4-3-default-and-trim-env-var-processors

Tweet this

Comments

Good news!
Realy good news. Especially in relation with the 'file' processor. I had to implement 'trim' by myself before.
Next feature should be the 'rawurlencode' processor and an 'escape' processor for masking "%" characters.

Comments are closed.

To ensure that comments stay relevant, they are closed for old posts.