Skip to content

Commit 5d4b335

Browse files
committed
minor #8722 Updated the best practice article about config (javiereguiluz, weaverryan)
This PR was merged into the 4.0 branch. Discussion ---------- Updated the best practice article about config This was the only remaining article to be updated in "Best Practices". Related to this: * The main config.rst is being updated in ~~#8588~~ * Some config/* articles are being updated in ~~#8647~~. * I'm going to update the rest of config/* articles. Commits ------- 175a4e8 minor tweaks b973f0c Updated the best practice article about config
2 parents f37ee12 + 175a4e8 commit 5d4b335

File tree

1 file changed

+36
-73
lines changed

1 file changed

+36
-73
lines changed

best_practices/configuration.rst

Lines changed: 36 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,33 @@ three parts.
1111
Infrastructure-Related Configuration
1212
------------------------------------
1313

14+
These are the options that change from one machine to another (e.g. from your
15+
development machine to the production server) but which don't change the
16+
application behavior.
17+
1418
.. best-practice::
1519

16-
Define the infrastructure-related configuration options in the
17-
``app/config/parameters.yml`` file.
20+
Define the infrastructure-related configuration options as environment
21+
variables. During development, use the ``.env`` file at the root of your
22+
project to set these.
1823

19-
The default ``parameters.yml`` file follows this recommendation and defines the
20-
options related to the database and mail server infrastructure:
24+
By default, Symfony adds these types of options to the ``.env`` file when
25+
installing new dependencies in the app:
2126

22-
.. code-block:: yaml
27+
.. code-block:: bash
2328
24-
# app/config/parameters.yml
25-
parameters:
26-
database_driver: pdo_mysql
27-
database_host: 127.0.0.1
28-
database_port: ~
29-
database_name: symfony
30-
database_user: root
31-
database_password: ~
29+
# .env
30+
###> doctrine/doctrine-bundle ###
31+
DATABASE_URL=sqlite:///%kernel.project_dir%/var/data/blog.sqlite
32+
###< doctrine/doctrine-bundle ###
3233
33-
mailer_transport: smtp
34-
mailer_host: 127.0.0.1
35-
mailer_user: ~
36-
mailer_password: ~
34+
###> symfony/swiftmailer-bundle ###
35+
MAILER_URL=smtp://localhost?encryption=ssl&auth_mode=login&username=&password=
36+
###< symfony/swiftmailer-bundle ###
3737
38-
# ...
38+
# ...
3939
40-
These options aren't defined inside the ``app/config/config.yml`` file because
40+
These options aren't defined inside the ``config/services.yaml`` file because
4141
they have nothing to do with the application's behavior. In other words, your
4242
application doesn't care about the location of your database or the credentials
4343
to access to it, as long as the database is correctly configured.
@@ -49,37 +49,32 @@ Canonical Parameters
4949

5050
.. best-practice::
5151

52-
Define all your application's parameters in the
53-
``app/config/parameters.yml.dist`` file.
52+
Define all your application's env vars in the ``.env.dist`` file.
5453

55-
Symfony includes a configuration file called ``parameters.yml.dist``, which
56-
stores the canonical list of configuration parameters for the application.
54+
Symfony includes a configuration file called ``.env.dist`` at the project root,
55+
which stores the canonical list of environment variables for the application.
5756

58-
Whenever a new configuration parameter is defined for the application, you
59-
should also add it to this file and submit the changes to your version control
60-
system. Then, whenever a developer updates the project or deploys it to a server,
61-
Symfony will check if there is any difference between the canonical
62-
``parameters.yml.dist`` file and your local ``parameters.yml`` file. If there
63-
is a difference, Symfony will ask you to provide a value for the new parameter
64-
and it will add it to your local ``parameters.yml`` file.
57+
Whenever a new env var is defined for the application, you should also add it to
58+
this file and submit the changes to your version control system so your
59+
workmates can update their ``.env`` files.
6560

6661
Application-Related Configuration
6762
---------------------------------
6863

6964
.. best-practice::
7065

7166
Define the application behavior related configuration options in the
72-
``app/config/config.yml`` file.
67+
``config/services.yaml`` file.
7368

74-
The ``config.yml`` file contains the options used by the application to modify
75-
its behavior, such as the sender of email notifications, or the enabled
76-
`feature toggles`_. Defining these values in ``parameters.yml`` file would
77-
add an extra layer of configuration that's not needed because you don't need
78-
or want these configuration values to change on each server.
69+
The ``services.yaml`` file contains the options used by the application to
70+
modify its behavior, such as the sender of email notifications, or the enabled
71+
`feature toggles`_. Defining these values in ``.env`` file would add an extra
72+
layer of configuration that's not needed because you don't need or want these
73+
configuration values to change on each server.
7974

80-
The configuration options defined in the ``config.yml`` file usually vary from
81-
one :doc:`environment </configuration/environments>` to another. That's
82-
why Symfony already includes ``app/config/config_dev.yml`` and ``app/config/config_prod.yml``
75+
The configuration options defined in the ``services.yaml`` may vary from one
76+
:doc:`environment </configuration/environments>` to another. That's why Symfony
77+
supports defining ``config/services_dev.yaml`` and ``config/services_prod.yaml``
8378
files so that you can override specific values for each environment.
8479

8580
Constants vs Configuration Options
@@ -99,7 +94,7 @@ to control the number of posts to display on the blog homepage:
9994

10095
.. code-block:: yaml
10196
102-
# app/config/config.yml
97+
# config/services.yaml
10398
parameters:
10499
homepage.num_items: 10
105100
@@ -167,7 +162,7 @@ just one or two words to describe the purpose of the parameter:
167162

168163
.. code-block:: yaml
169164
170-
# app/config/config.yml
165+
# config/services.yaml
171166
parameters:
172167
# don't do this: 'dir' is too generic and it doesn't convey any meaning
173168
app.dir: '...'
@@ -178,38 +173,6 @@ just one or two words to describe the purpose of the parameter:
178173
app.dir.contents: '...'
179174
app.contents-dir: '...'
180175
181-
Semantic Configuration: Don't Do It
182-
-----------------------------------
183-
184-
.. best-practice::
185-
186-
Don't define a semantic dependency injection configuration for your bundles.
187-
188-
As explained in :doc:`/bundles/extension` article, Symfony bundles
189-
have two choices on how to handle configuration: normal service configuration
190-
through the ``services.yml`` file and semantic configuration through a special
191-
``*Extension`` class.
192-
193-
Although semantic configuration is much more powerful and provides nice features
194-
such as configuration validation, the amount of work needed to define that
195-
configuration isn't worth it for bundles that aren't meant to be shared as
196-
third-party bundles.
197-
198-
Moving Sensitive Options Outside of Symfony Entirely
199-
----------------------------------------------------
200-
201-
When dealing with sensitive options, like database credentials, we also recommend
202-
that you store them outside the Symfony project and make them available
203-
through environment variables:
204-
205-
.. code-block:: yaml
206-
207-
# app/config/config.yml
208-
doctrine:
209-
dbal:
210-
# ...
211-
password: "%env(DB_PASSWORD)%"
212-
213176
----
214177

215178
Next: :doc:`/best_practices/business-logic`

0 commit comments

Comments
 (0)