Skip to content

Commit b973f0c

Browse files
committed
Updated the best practice article about config
1 parent b71d2c0 commit b973f0c

File tree

1 file changed

+35
-73
lines changed

1 file changed

+35
-73
lines changed

best_practices/configuration.rst

Lines changed: 35 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,32 @@ 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 in the ``.env`` file at the root of the project.
1822

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

22-
.. code-block:: yaml
26+
.. code-block:: bash
2327
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: ~
28+
# .env
29+
###> doctrine/doctrine-bundle ###
30+
DATABASE_URL=sqlite:///%kernel.project_dir%/var/data/blog.sqlite
31+
###< doctrine/doctrine-bundle ###
3232
33-
mailer_transport: smtp
34-
mailer_host: 127.0.0.1
35-
mailer_user: ~
36-
mailer_password: ~
33+
###> symfony/swiftmailer-bundle ###
34+
MAILER_URL=smtp://localhost?encryption=ssl&auth_mode=login&username=&password=
35+
###< symfony/swiftmailer-bundle ###
3736
38-
# ...
37+
# ...
3938
40-
These options aren't defined inside the ``app/config/config.yml`` file because
39+
These options aren't defined inside the ``config/services.yaml`` file because
4140
they have nothing to do with the application's behavior. In other words, your
4241
application doesn't care about the location of your database or the credentials
4342
to access to it, as long as the database is correctly configured.
@@ -49,37 +48,32 @@ Canonical Parameters
4948

5049
.. best-practice::
5150

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

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

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.
56+
Whenever a new env var is defined for the application, you should also add it to
57+
this file and submit the changes to your version control system so your
58+
workmates can update their ``.env`` files.
6559

6660
Application-Related Configuration
6761
---------------------------------
6862

6963
.. best-practice::
7064

7165
Define the application behavior related configuration options in the
72-
``app/config/config.yml`` file.
66+
``config/services.yaml`` file.
7367

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.
68+
The ``services.yaml`` file contains the options used by the application to
69+
modify its behavior, such as the sender of email notifications, or the enabled
70+
`feature toggles`_. Defining these values in ``.env`` file would add an extra
71+
layer of configuration that's not needed because you don't need or want these
72+
configuration values to change on each server.
7973

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``
74+
The configuration options defined in the ``services.yaml`` may vary from one
75+
:doc:`environment </configuration/environments>` to another. That's why Symfony
76+
supports defining ``config/services_dev.yaml`` and ``config/services_prod.yaml``
8377
files so that you can override specific values for each environment.
8478

8579
Constants vs Configuration Options
@@ -99,7 +93,7 @@ to control the number of posts to display on the blog homepage:
9993

10094
.. code-block:: yaml
10195
102-
# app/config/config.yml
96+
# config/services.yaml
10397
parameters:
10498
homepage.num_items: 10
10599
@@ -167,7 +161,7 @@ just one or two words to describe the purpose of the parameter:
167161

168162
.. code-block:: yaml
169163
170-
# app/config/config.yml
164+
# config/services.yaml
171165
parameters:
172166
# don't do this: 'dir' is too generic and it doesn't convey any meaning
173167
app.dir: '...'
@@ -178,38 +172,6 @@ just one or two words to describe the purpose of the parameter:
178172
app.dir.contents: '...'
179173
app.contents-dir: '...'
180174
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-
213175
----
214176

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

0 commit comments

Comments
 (0)