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