@@ -20,11 +20,11 @@ directory, which has this default structure:
20
20
The ``routes.yaml `` file defines the :doc: `routing configuration </routing >`;
21
21
the ``services.yaml `` file configures the services of the
22
22
:doc: `service container </service_container >`; the ``bundles.php `` file enables/
23
- disables packages in your application and it's managed automatically by
23
+ disables packages in your application and is managed automatically by
24
24
:doc: `Symfony Flex </setup/flex >`.
25
25
26
- The most important element inside `` config/ `` is the ``packages/ `` directory,
27
- which stores the configuration of every package installed in your application.
26
+ You'll be working most in the ``config/ packages/ `` directory. This directory
27
+ stores the configuration of every package installed in your application.
28
28
Packages (also called "bundles" in Symfony and "plugins/modules" in other
29
29
projects) add ready-to-use features to your projects.
30
30
@@ -57,8 +57,8 @@ and throughout the Symfony documentation, all configuration examples will be
57
57
shown in these three formats.
58
58
59
59
There isn't any practical difference between formats. In fact, Symfony
60
- transforms all of them into PHP before running the application, so there's not
61
- even any performance difference between them.
60
+ transforms and caches all of them into PHP before running the application, so
61
+ there's not even any performance difference between them.
62
62
63
63
YAML is used by default when installing packages because it's concise and very
64
64
readable. These are the main advantages and disadvantages of each format:
@@ -188,8 +188,8 @@ reusable configuration value. By convention, parameters are defined under the
188
188
// ...
189
189
190
190
Once defined, you can reference this parameter value from any other
191
- configuration file using a special syntax: wrap the parameter name with two
192
- `` % `` (e.g. ``%app.admin_email% ``):
191
+ configuration file using a special syntax: wrap the parameter name in two `` % ``
192
+ (e.g. ``%app.admin_email% ``):
193
193
194
194
.. configuration-block ::
195
195
@@ -215,9 +215,9 @@ configuration file using a special syntax: wrap the parameter name with two
215
215
https://symfony.com/schema/dic/symfony/symfony-1.0.xsd" >
216
216
217
217
<!-- any string surrounded by two % is replaced by that parameter value -->
218
- <some-package email-address =" %app.admin_email%" >
218
+ <some-package : config email-address =" %app.admin_email%" >
219
219
<!-- ... -->
220
- </some-package >
220
+ </some-package : config >
221
221
</container >
222
222
223
223
.. code-block :: php
@@ -258,26 +258,19 @@ to behave differently at different times:
258
258
* After deploying to **production **, you want that same application to be
259
259
optimized for speed and only log errors.
260
260
261
- In Symfony this is implemented with **environments **, which is a set of
262
- configuration files that define the application behavior. A typical Symfony
263
- application begins with three environments: ``dev `` (for local development),
264
- ``prod `` (for production servers) and ``test `` (for
265
- :doc: `automated tests </testing >`). Each of them defines its own configuration
266
- files:
261
+ The files stored in ``config/packages/ `` are used by Symfony to configure the
262
+ :doc: `application services </service_container >`. In other words, you can change
263
+ the application behavior by changing which configuration files are loaded.
264
+ That's the idea of Symfony's **configuration environments **.
267
265
268
- * for the ``dev `` environment: ``config/packages/dev/ ``
269
- * for the ``prod `` environment: ``config/packages/prod/ ``
270
- * for the ``test `` environment: ``config/packages/test/ ``
271
-
272
- In reality, each environment differs only somewhat from others. This means that
273
- all environments share a large base of common configurations, which is put in
274
- files directly in the ``config/packages/ `` directory.
275
-
276
- When running the application, Symfony loads the configuration files in this
277
- order (the last files can override the values set in the previous ones):
266
+ A typical Symfony application begins with three environments: ``dev `` (for local
267
+ development), ``prod `` (for production servers) and ``test `` (for
268
+ :doc: `automated tests </testing >`). When running the application, Symfony loads
269
+ the configuration files in this order (the last files can override the values
270
+ set in the previous ones):
278
271
279
272
#. ``config/packages/*.yaml `` (and ``.xml `` and ``*.php `` files too);
280
- #. ``config/packages/<environment>/*.yaml `` (and ``.xml `` and ``*.php `` files too);
273
+ #. ``config/packages/<environment-name >/*.yaml `` (and ``.xml `` and ``*.php `` files too);
281
274
#. ``config/packages/services.yaml `` (and ``services.xml `` and ``services.php `` files too);
282
275
283
276
Take the ``framework `` package, installed by default, as an example:
@@ -292,6 +285,10 @@ Take the ``framework`` package, installed by default, as an example:
292
285
is loaded to override some of the settings previously configured in
293
286
``config/packages/framework.yaml ``.
294
287
288
+ In reality, each environment differs only somewhat from others. This means that
289
+ all environments share a large base of common configurations, which is put in
290
+ files directly in the ``config/packages/ `` directory.
291
+
295
292
.. seealso ::
296
293
297
294
See the ``configureContainer() `` method of
@@ -311,10 +308,10 @@ Open the ``.env`` file and edit the value of the ``APP_ENV`` variable to change
311
308
the environment in which the application runs. For example, to run the
312
309
application in production:
313
310
314
- `` ` bash
315
- # .env
316
- APP_ENV=prod
317
- ` ``
311
+ .. code-block :: bash
312
+
313
+ # .env
314
+ APP_ENV=prod
318
315
319
316
This value is used both for the web and for the console commands. However, you
320
317
can override it for commands by setting the ``APP_ENV `` value before running them:
@@ -336,9 +333,9 @@ Creating a New Environment
336
333
~~~~~~~~~~~~~~~~~~~~~~~~~~
337
334
338
335
The default three environments provided by Symfony are enough for most projects,
339
- but you can define more environments if you want to . For example, this is how
340
- you can define a ``staging `` environment where the client can test the project
341
- before going to production:
336
+ but you can define your own environments too . For example, this is how you can
337
+ define a ``staging `` environment where the client can test the project before
338
+ going to production:
342
339
343
340
#. Create a configuration directory with the same name as the environment (in
344
341
this case, ``config/packages/staging/ ``);
@@ -349,20 +346,27 @@ before going to production:
349
346
#. Select the ``staging `` environment using the ``APP_ENV `` env var as explained
350
347
in the previous section.
351
348
349
+ .. tip ::
350
+
351
+ It's common for environments to be similar between each other, so you can
352
+ use `symbolic links `_ between ``config/packages/<environment-name>/ ``
353
+ directories to reuse the same configuration.
354
+
352
355
.. _config-env-vars :
353
356
354
357
Configuration Based on Environment Variables
355
358
--------------------------------------------
356
359
357
360
Using `environment variables `_ (or "env vars" for short) is a common practice to
358
- configure sensitive options such as credentials and passwords. Instead of
359
- defining the values of those options in the configuration files, you add a
360
- reference to an environment variable which will be resolved at runtime (only
361
- once per request, to not impact performance).
361
+ configure options that depend on where the application is run (e.g. the database
362
+ credentials are usually different in production and in your local machine).
363
+
364
+ Instead of defining those as regular options, you can define them as environment
365
+ variables and reference them in the configuration files using the special syntax
366
+ ``%env(ENV_VAR_NAME)% ``. The values of these options are resolved at runtime
367
+ (only once per request, to not impact performance).
362
368
363
- Environment variables can be used in any configuration file and they are
364
- referenced with the special syntax ``%env(ENV_VAR_NAME)% ``. This example shows
365
- how to configure the database connection using an env var:
369
+ This example shows how to configure the database connection using an env var:
366
370
367
371
.. configuration-block ::
368
372
@@ -588,4 +592,5 @@ Learn more
588
592
configuration/*
589
593
590
594
.. _`environment variables` : https://en.wikipedia.org/wiki/Environment_variable
595
+ .. _`symbolic links` : https://en.wikipedia.org/wiki/Symbolic_link
591
596
.. _`utilities to manage env vars` : https://symfony.com/doc/master/cloud/cookbooks/env.html
0 commit comments