@@ -102,8 +102,11 @@ will be used whenever the corresponding environment variable is *not* found:
102
102
Environment Variable Processors
103
103
-------------------------------
104
104
105
- When using environment variables they are always strings by default, but sometimes
106
- you will want to have specific types so that they match the types expected by your code.
105
+ The values of the environment variables are considered strings by def
8000
ault.
106
+ However, your code may expect other data types, like integers or booleans.
107
+ Symfony solves this problem with *processors *, which modify the contents of the
108
+ given environment variables. The following example uses the integer processor to
109
+ turn the value of the ``HTTP_PORT `` env var into an integer:
107
110
108
111
.. configuration-block ::
109
112
@@ -141,109 +144,111 @@ you will want to have specific types so that they match the types expected by yo
141
144
)
142
145
));
143
146
144
- A number of different types are supported :
147
+ Symfony provides the following env var processors :
145
148
146
149
``env(string:FOO) ``
147
- Casts ``FOO `` to a string
150
+ Casts ``FOO `` to a string:
148
151
149
- .. code-block :: php
152
+ .. code-block :: yaml
150
153
151
- parameters:
152
- env(SECRET): "some_secret"
153
- framework:
154
- secret: '%env(string:SECRET)%'
154
+ parameters :
155
+ env(SECRET) : " some_secret"
156
+ framework :
157
+ secret : ' %env(string:SECRET)%'
155
158
156
159
``env(bool:FOO) ``
157
- Casts ``FOO `` to a bool
160
+ Casts ``FOO `` to a bool:
158
161
159
- .. code-block :: php
162
+ .. code-block :: yaml
160
163
161
- parameters:
162
- env(HTTP_METHOD_OVERRIDE): "true"
163
- framework:
164
- http_method_override: '%env(bool:HTTP_METHOD_OVERRIDE)%'
164
+ parameters :
165
+ env(HTTP_METHOD_OVERRIDE) : " true"
166
+ framework :
167
+ http_method_override : ' %env(bool:HTTP_METHOD_OVERRIDE)%'
165
168
166
169
``env(int:FOO) ``
167
- Casts ``FOO `` to an int
170
+ Casts ``FOO `` to an int.
168
171
169
172
``env(float:FOO) ``
170
- Casts ``FOO `` to an float
173
+ Casts ``FOO `` to an float.
171
174
172
175
``env(const:FOO) ``
173
- Finds the const value named in ``FOO ``
176
+ Finds the const value named in ``FOO ``:
174
177
175
- .. code-block :: php
178
+ .. code-block :: yaml
176
179
177
- parameters:
178
- env(HEALTH_CHECK_METHOD): "Symfony\Component\HttpFoundation\Request:METHOD_HEAD"
179
- security:
180
- access_control:
181
- - { path: '^/health-check$', methods: '%env(const:HEALTH_CHECK_METHOD)%' }
180
+ parameters :
181
+ env(HEALTH_CHECK_METHOD) : " Symfony\C omponent\H ttpFoundation\R equest:METHOD_HEAD"
182
+ security :
183
+ access_control :
184
+ - { path: '^/health-check$', methods: '%env(const:HEALTH_CHECK_METHOD)%' }
182
185
183
186
``env(base64:FOO) ``
184
- Decodes ``FOO `` that is a base64 encoded string
185
-
187
+ Decodes the content of ``FOO ``, which is a base64 encoded string.
188
+
186
189
``env(json:FOO) ``
187
- Decodes ``FOO `` that is a json encoded string into either an array or ``null ``
190
+ Decodes the content of ``FOO ``, which is a JSON encoded string. It returns
191
+ either an array or ``null ``:
188
192
189
- .. code-block :: php
193
+ .. code-block :: yaml
194
+
195
+ parameters :
196
+ env(TRUSTED_HOSTS) : " ['10.0.0.1', '10.0.0.2']"
197
+ framework :
198
+ trusted_hosts : ' %env(json:TRUSTED_HOSTS)%'
190
199
191
- parameters:
192
- env(TRUSTED_HOSTS): "['10.0.0.1', '10.0.0.2']"
193
- framework:
194
- trusted_hosts: '%env(json:TRUSTED_HOSTS)%'
195
-
196
200
``env(resolve:FOO) ``
197
- Resolves references in the string ``FOO `` to other parameters
201
+ Replaces the string ``FOO `` by the value of a config parameter with the
202
+ same name:
198
203
199
- .. code-block :: php
204
+ .. code-block :: yaml
200
205
201
- parameters:
202
- env(HOST): '10.0.0.1'
203
- env(SENTRY_DSN): "http://%env(HOST)%/project"
204
- sentry:
205
- dsn: '%env(resolve:SENTRY_DSN)%'
206
+ parameters :
207
+ env(HOST) : ' 10.0.0.1'
208
+ env(SENTRY_DSN) : " http://%env(HOST)%/project"
209
+ sentry :
210
+ dsn : ' %env(resolve:SENTRY_DSN)%'
206
211
207
212
``env(csv:FOO) ``
208
- Decodes ``FOO `` that is a single row of comma seperated values
213
+ Decodes the content of ``FOO ``, which is a CSV-encoded string:
209
214
210
- .. code-block :: php
215
+ .. code-block :: yaml
211
216
212
- parameters:
213
- env(TRUSTED_HOSTS): "10.0.0.1, 10.0.0.2"
214
- framework:
215
- trusted_hosts: '%env(csv:TRUSTED_HOSTS)%'
217
+ parameters :
218
+ env(TRUSTED_HOSTS) : " 10.0.0.1, 10.0.0.2"
219
+ framework :
220
+ trusted_hosts : ' %env(csv:TRUSTED_HOSTS)%'
216
221
217
222
``env(file:FOO) ``
218
- Reads the contents of a file named in ``FOO ``
223
+ Returns the contents of a file whose path is the value of the ``FOO `` env var:
219
224
220
- .. code-block :: php
225
+ .. code-block :: yaml
221
226
222
- parameters:
223
- env(AUTH_FILE): "auth.json"
224
- google:
225
- auth: '%env(file:AUTH_FILE)%'
226
-
227
- It is also possible to combine the processors:
227
+ parameters :
228
+ env(AUTH_FILE) : " ../config/auth.json"
229
+ google :
230
+ auth : ' %env(file:AUTH_FILE)%'
228
231
229
- ``env(json:file:FOO) ``
230
- Reads the contents of a file named in ``FOO ``, and then decode it from json, resulting in an array or ``null ``
232
+ It is also possible to combine any number of processors:
231
233
232
- .. code-block :: php
234
+ .. code-block :: yaml
233
235
234
236
parameters :
235
- env(AUTH_FILE): "%kernel.root% /auth.json"
237
+ env(AUTH_FILE) : " %kernel.project_dir%/config /auth.json"
236
238
google :
237
- auth: '%env(file:resolve:AUTH_FILE)%'
238
-
239
+ # 1. gets the value of the AUTH_FILE env var
240
+ # 2. replaces the values of any config param to get the config path
241
+ # 3. gets the content of the file stored in that path
242
+ # 4. JSON-decodes the content of the file and returns it
243
+ auth : ' %env(json:file:resolve:AUTH_FILE)%'
239
244
240
245
Custom Environment Variable Processors
241
246
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
242
247
243
- Its possible to add further processors for environment variables. You just need
244
- to add an implementation of ` Symfony\Component\DependencyInjection\EnvVarProcessorInterface `.
245
-
246
- .. code-block :: php
248
+ It's also possible to add your own processors for environment variables. First,
249
+ create a class that implements
250
+ :class: ` Symfony \\ Component \\ DependencyInjection \\ EnvVarProcessorInterface ` and
251
+ then, define a service for that class::
247
252
248
253
class LowercasingEnvVarProcessor implements EnvVarProcessorInterface
249
254
{
@@ -269,8 +274,6 @@ to add an implementation of `Symfony\Component\DependencyInjection\EnvVarProcess
269
274
}
270
275
}
271
276
272
-
273
-
274
277
.. _configuration-env-var-in-prod :
275
278
276
279
Configuring Environment Variables in Production
0 commit comments