@@ -140,7 +140,7 @@ Environment Variable Processors
140140.. versionadded :: 3.4
141141 Environment variable processors were introduced in Symfony 3.4.
142142
143- The values of the environment variables are considered strings by default.
143+ The values of environment variables are considered strings by default.
144144However, your code may expect other data types, like integers or booleans.
145145Symfony solves this problem with *processors *, which modify the contents of the
146146given environment variables. The following example uses the integer processor to
@@ -159,7 +159,6 @@ turn the value of the ``HTTP_PORT`` env var into an integer:
159159
160160 <!-- config/packages/framework.xml -->
161161 <?xml version =" 1.0" encoding =" UTF-8" ?>
162-
163162 <container xmlns =" http://symfony.com/schema/dic/services"
164163 xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
165164 xmlns : framework =" http://symfony.com/schema/dic/symfony"
@@ -169,57 +168,152 @@ turn the value of the ``HTTP_PORT`` env var into an integer:
169168 http://symfony.com/schema/dic/symfony/symfony-1.0.xsd" >
170169
171170 <framework : config >
172- <framework : router http_port =" %env(int:HTTP_PORT)%" />
171+ <framework : router http-port =" %env(int:HTTP_PORT)%" />
173172 </framework : config >
174173 </container >
175174
176175 .. code-block :: php
177176
178- // config/packages/doctrine .php
177+ // config/packages/framework .php
179178 $container->loadFromExtension('framework', array(
180179 'router' => array(
181180 'http_port' => '%env(int:HTTP_PORT)%',
182- )
181+ ),
183182 ));
184183
185184 Symfony provides the following env var processors:
186185
187186``env(string:FOO) ``
188187 Casts ``FOO `` to a string:
189188
190- .. code -block :: yaml
189+ .. configuration -block ::
191190
192- parameters :
193- env(SECRET) : " some_secret"
194- framework :
195- secret : ' %env(string:SECRET)%'
191+ .. code-block :: yaml
192+
193+ # config/packages/framework.yaml
194+ parameters :
195+ env(SECRET) : ' some_secret'
196+ framework :
197+ secret : ' %env(string:SECRET)%'
198+
199+ .. code-block :: xml
200+
201+ <!-- config/packages/framework.xml -->
202+ <?xml version =" 1.0" encoding =" UTF-8" ?>
203+ <container xmlns =" http://symfony.com/schema/dic/services"
204+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
205+ xmlns : framework =" http://symfony.com/schema/dic/symfony"
206+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
207+ http://symfony.com/schema/dic/services/services-1.0.xsd
208+ http://symfony.com/schema/dic/symfony
209+ http://symfony.com/schema/dic/symfony/symfony-1.0.xsd" >
210+
211+ <parameters >
212+ <parameter key =" env(SECRET)" >some_secret</parameter >
213+ </parameters >
214+
215+ <framework : config secret =" %env(string:SECRET)%" />
216+ </container >
217+
218+ .. code-block :: php
219+
220+ // config/packages/framework.php
221+ $container->setParameter('env(SECRET)', 'some_secret');
222+ $container->loadFromExtension('framework', array(
223+ 'secret' => '%env(string:SECRET)%',
224+ ));
196225
197226 ``env(bool:FOO) ``
198227 Casts ``FOO `` to a bool:
199228
200- .. code -block :: yaml
229+ .. configuration -block ::
201230
202- parameters :
203- env(HTTP_METHOD_OVERRIDE) : " true"
204- framework :
205- http_method_override : ' %env(bool:HTTP_METHOD_OVERRIDE)%'
231+ .. code-block :: yaml
232+
233+ # config/packages/framework.yaml
234+ parameters :
235+ env(HTTP_METHOD_OVERRIDE) : ' true'
236+ framework :
237+ http_method_override : ' %env(bool:HTTP_METHOD_OVERRIDE)%'
238+
239+ .. code-block :: xml
240+
241+ <!-- config/packages/framework.xml -->
242+ <?xml version =" 1.0" encoding =" UTF-8" ?>
243+ <container xmlns =" http://symfony.com/schema/dic/services"
244+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
245+ xmlns : framework =" http://symfony.com/schema/dic/symfony"
246+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
247+ http://symfony.com/schema/dic/services/services-1.0.xsd
248+ http://symfony.com/schema/dic/symfony
249+ http://symfony.com/schema/dic/symfony/symfony-1.0.xsd" >
250+
251+ <parameters >
252+ <parameter key =" env(HTTP_METHOD_OVERRIDE)" >true</parameter >
253+ </parameters >
254+
255+ <framework : config http-methode-override =" %env(bool:HTTP_METHOD_OVERRIDE)%" />
256+ </container >
257+
258+ .. code-block :: php
259+
260+ // config/packages/framework.php
261+ $container->setParameter('env(HTTP_METHOD_OVERRIDE)', 'true');
262+ $container->loadFromExtension('framework', array(
263+ 'http_method_override' => '%env(bool:HTTP_METHOD_OVERRIDE)%',
264+ ));
206265
207266 ``env(int:FOO) ``
208267 Casts ``FOO `` to an int.
209268
210269``env(float:FOO) ``
211- Casts ``FOO `` to an float.
270+ Casts ``FOO `` to a float.
212271
213272``env(const:FOO) ``
214273 Finds the const value named in ``FOO ``:
215274
216- .. code-block :: yaml
217-
218- parameters :
219- env(HEALTH_CHECK_METHOD) : " Symfony\C omponent\H ttpFoundation\R equest:METHOD_HEAD"
220- security :
221- access_control :
222- - { path: '^/health-check$', methods: '%env(const:HEALTH_CHECK_METHOD)%' }
275+ .. configuration-block ::
276+
277+ .. code-block :: yaml
278+
279+ # config/packages/security.yaml
280+ parameters :
281+ env(HEALTH_CHECK_METHOD) : ' Symfony\Component\HttpFoundation\Request::METHOD_HEAD'
282+ security :
283+ access_control :
284+ - { path: '^/health-check$', methods: '%env(const:HEALTH_CHECK_METHOD)%' }
285+
286+ .. code-block :: xml
287+
288+ <!-- config/packages/security.xml -->
289+ <?xml version =" 1.0" encoding =" UTF-8" ?>
290+ <container xmlns =" http://symfony.com/schema/dic/services"
291+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
292+ xmlns : security =" http://symfony.com/schema/dic/security"
293+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
294+ http://symfony.com/schema/dic/services/services-1.0.xsd" >
295+
296+ <parameters >
297+ <parameter key =" env(HEALTH_CHECK_METHOD)" >Symfony\Component\HttpFoundation\Request::METHOD_HEAD</parameter >
298+ </parameters >
299+
300+ <security : config >
301+ <rule path =" ^/health-check$" methods =" %env(const:HEALTH_CHECK_METHOD)%" />
302+ </security : config >
303+ </container >
304+
305+ .. code-block :: php
306+
307+ // config/packages/security.php
308+ $container->setParameter('env(HEALTH_CHECK_METHOD)', 'Symfony\Component\HttpFoundation\Request::METHOD_HEAD');
309+ $container->loadFromExtension('security', array(
310+ 'access_control' => array(
311+ array(
312+ 'path' => '^/health-check$',
313+ 'methods' => '%env(const:HEALTH_CHECK_METHOD)%',
314+ ),
315+ ),
316+ ));
223317
224318 ``env(base64:FOO) ``
225319 Decodes the content of ``FOO ``, which is a base64 encoded string.
@@ -228,24 +322,83 @@ Symfony provides the following env var processors:
228322 Decodes the content of ``FOO ``, which is a JSON encoded string. It returns
229323 either an array or ``null ``:
230324
231- .. code -block :: yaml
325+ .. configuration -block ::
232326
233- parameters :
234- env(TRUSTED_HOSTS) : " ['10.0.0.1', '10.0.0.2']"
235- framework :
236- trusted_hosts : ' %env(json:TRUSTED_HOSTS)%'
327+ .. code-block :: yaml
328+
329+ # config/packages/framework.yaml
330+ parameters :
331+ env(TRUSTED_HOSTS) : ' ["10.0.0.1", "10.0.0.2"]'
332+ framework :
333+ trusted_hosts : ' %env(json:TRUSTED_HOSTS)%'
334+
335+ .. code-block :: xml
336+
337+ <!-- config/packages/framework.xml -->
338+ <?xml version =" 1.0" encoding =" UTF-8" ?>
339+ <container xmlns =" http://symfony.com/schema/dic/services"
340+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
341+ xmlns : framework =" http://symfony.com/schema/dic/symfony"
342+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
343+ http://symfony.com/schema/dic/services/services-1.0.xsd
344+ http://symfony.com/schema/dic/symfony
345+ http://symfony.com/schema/dic/symfony/symfony-1.0.xsd" >
346+
347+ <parameters >
348+ <parameter key =" env(TRUSTED_HOSTS)" >["10.0.0.1", "10.0.0.2"]</parameter >
349+ </parameters >
350+
351+ <framework : config trusted-hosts =" %env(json:TRUSTED_HOSTS)%" />
352+ </container >
353+
354+ .. code-block :: php
355+
356+ // config/packages/framework.php
357+ $container->setParameter('env(TRUSTED_HOSTS)', '["10.0.0.1", "10.0.0.2"]');
358+ $container->loadFromExtension('framework', array(
359+ 'trusted_hosts' => '%env(json:TRUSTED_HOSTS)%',
360+ ));
237361
238362 ``env(resolve:FOO) ``
239363 Replaces the string ``FOO `` by the value of a config parameter with the
240364 same name:
241365
242- .. code -block :: yaml
366+ .. configuration -block ::
243367
244- parameters :
245- env(HOST) : ' 10.0.0.1'
246- env(SENTRY_DSN) : " http://%env(HOST)%/project"
247- sentry :
248- dsn : ' %env(resolve:SENTRY_DSN)%'
368+ .. code-block :: yaml
369+
370+ # config/packages/sentry.yaml
371+ parameters :
372+ env(HOST) : ' 10.0.0.1'
373+ env(SENTRY_DSN) : ' http://%env(HOST)%/project'
374+ sentry :
375+ dsn : ' %env(resolve:SENTRY_DSN)%'
376+
377+ .. code-block :: xml
378+
379+ <!-- config/packages/sentry.xml -->
380+ <?xml version =" 1.0" encoding =" UTF-8" ?>
381+ <container xmlns =" http://symfony.com/schema/dic/services"
382+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
383+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
384+ http://symfony.com/schema/dic/services/services-1.0.xsd" >
385+
386+ <parameters >
387+ <parameter key =" env(HOST)" >10.0.0.1</parameter >
388+ <parameter key =" env(SENTRY_DSN)" >http://%env(HOST)%/project</parameter >
389+ </parameters >
390+
391+ <sentry : config dsn =" %env(resolve:SENTRY_DSN)%" />
392+ </container >
393+
394+ .. code-block :: php
395+
396+ // config/packages/sentry.php
397+ $container->setParameter('env(HOST)', '10.0.0.1');
398+ $container->setParameter('env(SENTRY_DSN)', 'http://%env(HOST)%/project');
399+ $container->loadFromExtension('sentry', array(
400+ 'dsn' => '%env(resolve:SENTRY_DSN)%',
401+ ));
249402
250403 ``env(csv:FOO) ``
251404 Decodes the content of ``FOO ``, which is a CSV-encoded string:
@@ -263,12 +416,42 @@ Symfony provides the following env var processors:
263416``env(file:FOO) ``
264417 Returns the contents of a file whose path is the value of the ``FOO `` env var:
265418
266- .. code -block :: yaml
419+ .. configuration -block ::
267420
268- parameters :
269- env(AUTH_FILE) : " ../config/auth.json"
270- google :
271- auth : ' %env(file:AUTH_FILE)%'
421+ .. code-block :: yaml
422+
423+ # config/packages/framework.yaml
424+ parameters :
425+ env(AUTH_FILE) : ' ../config/auth.json'
426+ google :
427+ auth : ' %env(file:AUTH_FILE)%'
428+
429+ .. code-block :: xml
430+
431+ <!-- config/packages/framework.xml -->
432+ <?xml version =" 1.0" encoding =" UTF-8" ?>
433+ <container xmlns =" http://symfony.com/schema/dic/services"
434+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
435+ xmlns : framework =" http://symfony.com/schema/dic/symfony"
436+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
437+ http://symfony.com/schema/dic/services/services-1.0.xsd
438+ http://symfony.com/schema/dic/symfony
439+ http://symfony.com/schema/dic/symfony/symfony-1.0.xsd" >
440+
441+ <parameters >
442+ <parameter key =" env(AUTH_FILE)" >../config/auth.json</parameter >
443+ </parameters >
444+
445+ <google auth =" %env(file:AUTH_FILE)%" />
446+ </container >
447+
448+ .. code-block :: php
449+
450+ // config/packages/framework.php
451+ $container->setParameter('env(AUTH_FILE)', '../config/auth.json');
452+ $container->loadFromExtension('google', array(
453+ 'auth' => '%env(file:AUTH_FILE)%',
454+ ));
272455
273456 It is also possible to combine any number of processors:
274457
0 commit comments