@@ -140,7 +140,7 @@ Environment Variable Processors
140
140
.. versionadded :: 3.4
141
141
Environment variable processors were introduced in Symfony 3.4.
142
142
143
- The values of the environment variables are considered strings by default.
143
+ The values of environment variables are considered strings by default.
144
144
However, your code may expect other data types, like integers or booleans.
145
145
Symfony solves this problem with *processors *, which modify the contents of the
146
146
given 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:
159
159
160
160
<!-- config/packages/framework.xml -->
161
161
<?xml version =" 1.0" encoding =" UTF-8" ?>
162
-
163
162
<container xmlns =" http://symfony.com/schema/dic/services"
164
163
xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
165
164
xmlns : framework =" http://symfony.com/schema/dic/symfony"
@@ -169,57 +168,152 @@ turn the value of the ``HTTP_PORT`` env var into an integer:
169
168
http://symfony.com/schema/dic/symfony/symfony-1.0.xsd" >
170
169
171
170
<framework : config >
172
- <framework : router http_port =" %env(int:HTTP_PORT)%" />
171
+ <framework : router http-port =" %env(int:HTTP_PORT)%" />
173
172
</framework : config >
174
173
</container >
175
174
176
175
.. code-block :: php
177
176
178
- // config/packages/doctrine .php
177
+ // config/packages/framework .php
179
178
$container->loadFromExtension('framework', array(
180
179
'router' => array(
181
180
'http_port' => '%env(int:HTTP_PORT)%',
182
- )
181
+ ),
183
182
));
184
183
185
184
Symfony provides the following env var processors:
186
185
187
186
``env(string:FOO) ``
188
187
Casts ``FOO `` to a string:
189
188
190
- .. code -block :: yaml
189
+ .. configuration -block ::
191
190
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
+ ));
196
225
197
226
``env(bool:FOO) ``
198
227
Casts ``FOO `` to a bool:
199
228
200
- .. code -block :: yaml
229
+ .. configuration -block ::
201
230
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
+ ));
206
265
207
266
``env(int:FOO) ``
208
267
Casts ``FOO `` to an int.
209
268
210
269
``env(float:FOO) ``
211
- Casts ``FOO `` to an float.
270
+ Casts ``FOO `` to a float.
212
271
213
272
``env(const:FOO) ``
214
273
Finds the const value named in ``FOO ``:
215
274
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
+ ));
223
317
224
318
``env(base64:FOO) ``
225
319
Decodes the content of ``FOO ``, which is a base64 encoded string.
@@ -228,24 +322,83 @@ Symfony provides the following env var processors:
228
322
Decodes the content of ``FOO ``, which is a JSON encoded string. It returns
229
323
either an array or ``null ``:
230
324
231
- .. code -block :: yaml
325
+ .. configuration -block ::
232
326
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
+ ));
237
361
238
362
``env(resolve:FOO) ``
239
363
Replaces the string ``FOO `` by the value of a config parameter with the
240
364
same name:
241
365
242
- .. code -block :: yaml
366
+ .. configuration -block ::
243
367
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
+ ));
249
402
250
403
``env(csv:FOO) ``
251
404
Decodes the content of ``FOO ``, which is a CSV-encoded string:
@@ -263,12 +416,42 @@ Symfony provides the following env var processors:
263
416
``env(file:FOO) ``
264
417
Returns the contents of a file whose path is the value of the ``FOO `` env var:
265
418
266
- .. code -block :: yaml
419
+ .. configuration -block ::
267
420
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
+ ));
272
455
273
456
It is also possible to combine any number of processors:
274
457
0 commit comments