@@ -11,33 +11,32 @@ three parts.
11
11
Infrastructure-Related Configuration
12
12
------------------------------------
13
13
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
+
14
18
.. best-practice ::
15
19
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 .
18
22
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 :
21
25
22
- .. code-block :: yaml
26
+ .. code-block :: bash
23
27
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 ###
32
32
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 ###
37
36
38
- # ...
37
+ # ...
39
38
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
41
40
they have nothing to do with the application's behavior. In other words, your
42
41
application doesn't care about the location of your database or the credentials
43
42
to access to it, as long as the database is correctly configured.
@@ -49,37 +48,32 @@ Canonical Parameters
49
48
50
49
.. best-practice ::
51
50
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.
54
52
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.
57
55
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.
65
59
66
60
Application-Related Configuration
67
61
---------------------------------
68
62
69
63
.. best-practice ::
70
64
71
65
Define the application behavior related configuration options in the
72
- ``app/ config/config.yml `` file.
66
+ ``config/services.yaml `` file.
73
67
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.
79
73
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 ``
83
77
files so that you can override specific values for each environment.
84
78
85
79
Constants vs Configuration Options
@@ -99,7 +93,7 @@ to control the number of posts to display on the blog homepage:
99
93
100
94
.. code-block :: yaml
101
95
102
- # app/ config/config.yml
96
+ # config/services.yaml
103
97
parameters :
104
98
homepage.num_items : 10
105
99
@@ -167,7 +161,7 @@ just one or two words to describe the purpose of the parameter:
167
161
168
162
.. code-block :: yaml
169
163
170
- # app/ config/config.yml
164
+ # config/services.yaml
171
165
parameters :
172
166
# don't do this: 'dir' is too generic and it doesn't convey any meaning
173
167
app.dir : ' ...'
@@ -178,38 +172,6 @@ just one or two words to describe the purpose of the parameter:
178
172
app.dir.contents : ' ...'
179
173
app.contents-dir : ' ...'
180
174
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
-
213
175
----
214
176
215
177
Next: :doc: `/best_practices/business-logic `
0 commit comments