Environment Variable Processors

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:

  • YAML

    1. # config/packages/framework.yaml
    2. framework:
    3. router:
    4. http_port: '%env(int:HTTP_PORT)%'
  • XML

    1. <!-- config/packages/framework.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:framework="http://symfony.com/schema/dic/symfony"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd
    8. http://symfony.com/schema/dic/symfony
    9. https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
    10. <framework:config>
    11. <framework:router http-port="%env(int:HTTP_PORT)%"/>
    12. </framework:config>
    13. </container>
  • PHP

    1. // config/packages/framework.php
    2. $container->loadFromExtension('framework', [
    3. 'router' => [
    4. 'http_port' => '%env(int:HTTP_PORT)%',
    5. ],
    6. ]);

Built-In Environment Variable Processors

Symfony provides the following env var processors:

env(string:FOO)

Casts FOO to a string:

  • YAML

    1. # config/packages/framework.yaml
    2. parameters:
    3. env(SECRET): 'some_secret'
    4. framework:
    5. secret: '%env(string:SECRET)%'
  • XML

    1. <!-- config/packages/framework.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:framework="http://symfony.com/schema/dic/symfony"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd
    8. http://symfony.com/schema/dic/symfony
    9. https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
    10. <parameters>
    11. <parameter key="env(SECRET)">some_secret</parameter>
    12. </parameters>
    13. <framework:config secret="%env(string:SECRET)%"/>
    14. </container>
  • PHP

    1. // config/packages/framework.php
    2. $container->setParameter('env(SECRET)', 'some_secret');
    3. $container->loadFromExtension('framework', [
    4. 'secret' => '%env(string:SECRET)%',
    5. ]);

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):

  • YAML

    1. # config/packages/framework.yaml
    2. parameters:
    3. env(HTTP_METHOD_OVERRIDE): 'true'
    4. framework:
    5. http_method_override: '%env(bool:HTTP_METHOD_OVERRIDE)%'
  • XML

    1. <!-- config/packages/framework.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:framework="http://symfony.com/schema/dic/symfony"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd
    8. http://symfony.com/schema/dic/symfony
    9. https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
    10. <parameters>
    11. <parameter key="env(HTTP_METHOD_OVERRIDE)">true</parameter>
    12. </parameters>
    13. <framework:config http-method-override="%env(bool:HTTP_METHOD_OVERRIDE)%"/>
    14. </container>
  • PHP

    1. // config/packages/framework.php
    2. $container->setParameter('env(HTTP_METHOD_OVERRIDE)', 'true');
    3. $container->loadFromExtension('framework', [
    4. 'http_method_override' => '%env(bool:HTTP_METHOD_OVERRIDE)%',
    5. ]);

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:

  • YAML

    1. # config/packages/security.yaml
    2. parameters:
    3. env(HEALTH_CHECK_METHOD): 'Symfony\Component\HttpFoundation\Request::METHOD_HEAD'
    4. security:
    5. access_control:
    6. - { path: '^/health-check$', methods: '%env(const:HEALTH_CHECK_METHOD)%' }
  • XML

    1. <!-- config/packages/security.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:security="http://symfony.com/schema/dic/security"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd">
    8. <parameters>
    9. <parameter key="env(HEALTH_CHECK_METHOD)">Symfony\Component\HttpFoundation\Request::METHOD_HEAD</parameter>
    10. </parameters>
    11. <security:config>
    12. <rule path="^/health-check$" methods="%env(const:HEALTH_CHECK_METHOD)%"/>
    13. </security:config>
    14. </container>
  • PHP

    1. // config/packages/security.php
    2. $container->setParameter('env(HEALTH_CHECK_METHOD)', 'Symfony\Component\HttpFoundation\Request::METHOD_HEAD');
    3. $container->loadFromExtension('security', [
    4. 'access_control' => [
    5. [
    6. 'path' => '^/health-check$',
    7. 'methods' => '%env(const:HEALTH_CHECK_METHOD)%',
    8. ],
    9. ],
    10. ]);

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:

  • YAML

    1. # config/packages/framework.yaml
    2. parameters:
    3. env(TRUSTED_HOSTS): '["10.0.0.1", "10.0.0.2"]'
    4. framework:
    5. trusted_hosts: '%env(json:TRUSTED_HOSTS)%'
  • XML

    1. <!-- config/packages/framework.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:framework="http://symfony.com/schema/dic/symfony"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd
    8. http://symfony.com/schema/dic/symfony
    9. https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
    10. <parameters>
    11. <parameter key="env(TRUSTED_HOSTS)">["10.0.0.1", "10.0.0.2"]</parameter>
    12. </parameters>
    13. <framework:config trusted-hosts="%env(json:TRUSTED_HOSTS)%"/>
    14. </container>
  • PHP

    1. // config/packages/framework.php
    2. $container->setParameter('env(TRUSTED_HOSTS)', '["10.0.0.1", "10.0.0.2"]');
    3. $container->loadFromExtension('framework', [
    4. 'trusted_hosts' => '%env(json:TRUSTED_HOSTS)%',
    5. ]);

env(resolve:FOO)

If the content of FOO includes container parameters (with the syntax %parameter_name%), it replaces the parameters by their values:

  • YAML

    1. # config/packages/sentry.yaml
    2. parameters:
    3. env(HOST): '10.0.0.1'
    4. sentry_host: '%env(HOST)%'
    5. env(SENTRY_DSN): 'http://%sentry_host%/project'
    6. sentry:
    7. dsn: '%env(resolve:SENTRY_DSN)%'
  • XML

    1. <!-- config/packages/sentry.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xsi:schemaLocation="http://symfony.com/schema/dic/services
    6. https://symfony.com/schema/dic/services/services-1.0.xsd">
    7. <parameters>
    8. <parameter key="env(HOST)">10.0.0.1</parameter>
    9. <parameter key="sentry_host">%env(HOST)%</parameter>
    10. <parameter key="env(SENTRY_DSN)">http://%sentry_host%/project</parameter>
    11. </parameters>
    12. <sentry:config dsn="%env(resolve:SENTRY_DSN)%"/>
    13. </container>
  • PHP

    1. // config/packages/sentry.php
    2. $container->setParameter('env(HOST)', '10.0.0.1');
    3. $container->setParameter('sentry_host', '%env(HOST)%');
    4. $container->setParameter('env(SENTRY_DSN)', 'http://%sentry_host%/project');
    5. $container->loadFromExtension('sentry', [
    6. 'dsn' => '%env(resolve:SENTRY_DSN)%',
    7. ]);

env(csv:FOO)

Decodes the content of FOO, which is a CSV-encoded string:

  • YAML

    1. # config/packages/framework.yaml
    2. parameters:
    3. env(TRUSTED_HOSTS): "10.0.0.1,10.0.0.2"
    4. framework:
    5. trusted_hosts: '%env(csv:TRUSTED_HOSTS)%'
  • XML

    1. <!-- config/packages/framework.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:framework="http://symfony.com/schema/dic/symfony"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd
    8. http://symfony.com/schema/dic/symfony
    9. https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
    10. <parameters>
    11. <parameter key="env(TRUSTED_HOSTS)">10.0.0.1,10.0.0.2</parameter>
    12. </parameters>
    13. <framework:config trusted-hosts="%env(csv:TRUSTED_HOSTS)%"/>
    14. </container>
  • PHP

    1. // config/packages/framework.php
    2. $container->setParameter('env(TRUSTED_HOSTS)', '10.0.0.1,10.0.0.2');
    3. $container->loadFromExtension('framework', [
    4. 'trusted_hosts' => '%env(csv:TRUSTED_HOSTS)%',
    5. ]);

env(file:FOO)

Returns the contents of a file whose path is the value of the FOO env var:

  • YAML

    1. # config/packages/framework.yaml
    2. parameters:
    3. env(AUTH_FILE): '../config/auth.json'
    4. google:
    5. auth: '%env(file:AUTH_FILE)%'
  • XML

    1. <!-- config/packages/framework.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:framework="http://symfony.com/schema/dic/symfony"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd
    8. http://symfony.com/schema/dic/symfony
    9. https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
    10. <parameters>
    11. <parameter key="env(AUTH_FILE)">../config/auth.json</parameter>
    12. </parameters>
    13. <google auth="%env(file:AUTH_FILE)%"/>
    14. </container>
  • PHP

    1. // config/packages/framework.php
    2. $container->setParameter('env(AUTH_FILE)', '../config/auth.json');
    3. $container->loadFromExtension('google', [
    4. 'auth' => '%env(file:AUTH_FILE)%',
    5. ]);

env(require:FOO)

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

  • YAML

    1. # config/packages/framework.yaml
    2. parameters:
    3. env(PHP_FILE): '../config/.runtime-evaluated.php'
    4. app:
    5. auth: '%env(require:PHP_FILE)%'
  • XML

    1. <!-- config/packages/framework.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:framework="http://symfony.com/schema/dic/symfony"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd
    8. http://symfony.com/schema/dic/symfony
    9. https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
    10. <parameters>
    11. <parameter key="env(PHP_FILE)">../config/.runtime-evaluated.php</parameter>
    12. </parameters>
    13. <app auth="%env(require:PHP_FILE)%"/>
    14. </container>
  • PHP

    1. // config/packages/framework.php
    2. $container->setParameter('env(PHP_FILE)', '../config/.runtime-evaluated.php');
    3. $container->loadFromExtension('app', [
    4. 'auth' => '%env(require:PHP_FILE)%',
    5. ]);

New in version 4.3: The require processor was introduced in Symfony 4.3.

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.

  • YAML

    1. # config/packages/framework.yaml
    2. parameters:
    3. env(AUTH_FILE): '../config/auth.json'
    4. google:
    5. auth: '%env(trim:file:AUTH_FILE)%'
  • XML

    1. <!-- config/packages/framework.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:framework="http://symfony.com/schema/dic/symfony"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd
    8. http://symfony.com/schema/dic/symfony
    9. https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
    10. <parameters>
    11. <parameter key="env(AUTH_FILE)">../config/auth.json</parameter>
    12. </parameters>
    13. <google auth="%env(trim:file:AUTH_FILE)%"/>
    14. </container>
  • PHP

    1. // config/packages/framework.php
    2. $container->setParameter('env(AUTH_FILE)', '../config/auth.json');
    3. $container->loadFromExtension('google', [
    4. 'auth' => '%env(trim:file:AUTH_FILE)%',
    5. ]);

New in version 4.3: The trim processor was introduced in Symfony 4.3.

env(key:FOO:BAR)

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

  • YAML

    1. # config/services.yaml
    2. parameters:
    3. env(SECRETS_FILE): '/opt/application/.secrets.json'
    4. database_password: '%env(key:database_password:json:file:SECRETS_FILE)%'
    5. # if SECRETS_FILE contents are: {"database_password": "secret"} it returns "secret"
  • XML

    1. <!-- config/services.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:framework="http://symfony.com/schema/dic/symfony"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd
    8. http://symfony.com/schema/dic/symfony
    9. https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
    10. <parameters>
    11. <parameter key="env(SECRETS_FILE)">/opt/application/.secrets.json</parameter>
    12. <parameter key="database_password">%env(key:database_password:json:file:SECRETS_FILE)%</parameter>
    13. </parameters>
    14. </container>
  • PHP

    1. // config/services.php
    2. $container->setParameter('env(SECRETS_FILE)', '/opt/application/.secrets.json');
    3. $container->setParameter('database_password', '%env(key:database_password:json:file:SECRETS_FILE)%');

env(default:fallback_param:BAR)

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

  • YAML

    1. # config/services.yaml
    2. parameters:
    3. # if PRIVATE_KEY is not a valid file path, the content of raw_key is returned
    4. private_key: '%env(default:raw_key:file:PRIVATE_KEY)%'
    5. raw_key: '%env(PRIVATE_KEY)%'
  • XML

    1. <!-- config/services.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:framework="http://symfony.com/schema/dic/symfony"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd
    8. http://symfony.com/schema/dic/symfony
    9. https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
    10. <parameters>
    11. <!-- if PRIVATE_KEY is not a valid file path, the content of raw_key is returned -->
    12. <parameter key="private_key">%env(default:raw_key:file:PRIVATE_KEY)%</parameter>
    13. <parameter key="raw_key">%env(PRIVATE_KEY)%</parameter>
    14. </parameters>
    15. </container>
  • PHP

    1. // config/services.php
    2. // if PRIVATE_KEY is not a valid file path, the content of raw_key is returned
    3. $container->setParameter('private_key', '%env(default:raw_key:file:PRIVATE_KEY)%');
    4. $container->setParameter('raw_key', '%env(PRIVATE_KEY)%');

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

New in version 4.3: The default processor was introduced in Symfony 4.3.

env(url:FOO)

Parses an absolute URL and returns its components as an associative array.

  1. # .env
  2. MONGODB_URL="mongodb://db_user:[email protected]:27017/db_name"
  • YAML

    1. # config/packages/mongodb.yaml
    2. mongo_db_bundle:
    3. clients:
    4. default:
    5. hosts:
    6. - { host: '%env(key:host:url:MONGODB_URL)%', port: '%env(key:port:url:MONGODB_URL)%' }
    7. username: '%env(key:user:url:MONGODB_URL)%'
    8. password: '%env(key:pass:url:MONGODB_URL)%'
    9. connections:
    10. default:
    11. database_name: '%env(key:path:url:MONGODB_URL)%'
  • XML

    1. <!-- config/packages/mongodb.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xsi:schemaLocation="http://symfony.com/schema/dic/services
    6. https://symfony.com/schema/dic/services/services-1.0.xsd">
    7. <mongodb:config>
    8. <mongodb:client name="default" username="%env(key:user:url:MONGODB_URL)%" password="%env(key:pass:url:MONGODB_URL)%">
    9. <mongodb:host host="%env(key:host:url:MONGODB_URL)%" port="%env(key:port:url:MONGODB_URL)%"/>
    10. </mongodb:client>
    11. <mongodb:connections name="default" database_name="%env(key:path:url:MONGODB_URL)%"/>
    12. </mongodb:config>
    13. </container>
  • PHP

    1. // config/packages/mongodb.php
    2. $container->loadFromExtension('mongodb', [
    3. 'clients' => [
    4. 'default' => [
    5. 'hosts' => [
    6. [
    7. 'host' => '%env(key:host:url:MONGODB_URL)%',
    8. 'port' => '%env(key:port:url:MONGODB_URL)%',
    9. ],
    10. ],
    11. 'username' => '%env(key:user:url:MONGODB_URL)%',
    12. 'password' => '%env(key:pass:url:MONGODB_URL)%',
    13. ],
    14. ],
    15. 'connections' => [
    16. 'default' => [
    17. 'database_name' => '%env(key:path:url:MONGODB_URL)%',
    18. ],
    19. ],
    20. ]);

Caution

In order to ease extraction of the resource from the URL, the leading / is trimmed from the path component.

New in version 4.3: The url processor was introduced in Symfony 4.3.

env(query_string:FOO)

Parses the query string part of the given URL and returns its components as an associative array.

  1. # .env
  2. MONGODB_URL="mongodb://db_user:[email protected]:27017/db_name?timeout=3000"
  • YAML

    1. # config/packages/mongodb.yaml
    2. mongo_db_bundle:
    3. clients:
    4. default:
    5. # ...
    6. connectTimeoutMS: '%env(int:key:timeout:query_string:MONGODB_URL)%'
  • XML

    1. <!-- config/packages/mongodb.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xsi:schemaLocation="http://symfony.com/schema/dic/services
    6. https://symfony.com/schema/dic/services/services-1.0.xsd">
    7. <mongodb:config>
    8. <mongodb:client name="default" connectTimeoutMS="%env(int:key:timeout:query_string:MONGODB_URL)%"/>
    9. </mongodb:config>
    10. </container>
  • PHP

    1. // config/packages/mongodb.php
    2. $container->loadFromExtension('mongodb', [
    3. 'clients' => [
    4. 'default' => [
    5. // ...
    6. 'connectTimeoutMS' => '%env(int:key:timeout:query_string:MONGODB_URL)%',
    7. ],
    8. ],
    9. ]);

New in version 4.3: The query_string processor was introduced in Symfony 4.3.

It is also possible to combine any number of processors:

  • YAML

    1. # config/packages/framework.yaml
    2. parameters:
    3. env(AUTH_FILE): "%kernel.project_dir%/config/auth.json"
    4. google:
    5. # 1. gets the value of the AUTH_FILE env var
    6. # 2. replaces the values of any config param to get the config path
    7. # 3. gets the content of the file stored in that path
    8. # 4. JSON-decodes the content of the file and returns it
    9. auth: '%env(json:file:resolve:AUTH_FILE)%'
  • XML

    1. <!-- config/packages/framework.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:framework="http://symfony.com/schema/dic/symfony"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd
    8. http://symfony.com/schema/dic/symfony
    9. https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
    10. <parameters>
    11. <parameter key="env(AUTH_FILE)">%kernel.project_dir%/config/auth.json</parameter>
    12. </parameters>
    13. <!-- 1. gets the value of the AUTH_FILE env var -->
    14. <!-- 2. replaces the values of any config param to get the config path -->
    15. <!-- 3. gets the content of the file stored in that path -->
    16. <!-- 4. JSON-decodes the content of the file and returns it -->
    17. <google auth="%env(json:file:resolve:AUTH_FILE)%"/>
    18. </container>
  • PHP

    1. // config/packages/framework.php
    2. $container->setParameter('env(AUTH_FILE)', '%kernel.project_dir%/config/auth.json');
    3. // 1. gets the value of the AUTH_FILE env var
    4. // 2. replaces the values of any config param to get the config path
    5. // 3. gets the content of the file stored in that path
    6. // 4. JSON-decodes the content of the file and returns it
    7. $container->loadFromExtension('google', [
    8. 'auth' => '%env(json:file:resolve:AUTH_FILE)%',
    9. ]);

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:

  1. use Symfony\Component\DependencyInjection\EnvVarProcessorInterface;
  2. class LowercasingEnvVarProcessor implements EnvVarProcessorInterface
  3. {
  4. public function getEnv($prefix, $name, \Closure $getEnv)
  5. {
  6. $env = $getEnv($name);
  7. return strtolower($env);
  8. }
  9. public static function getProvidedTypes()
  10. {
  11. return [
  12. 'lowercase' => 'string',
  13. ];
  14. }
  15. }

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.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.