|
1 | 1 | .. index:: |
2 | | - single: Environments; External parameters |
| 2 | + single: Environment Variables; env vars |
3 | 3 |
|
4 | | -How to Set external Parameters in the Service Container |
5 | | -======================================================= |
| 4 | +How to Configure Symfony With Environment Variables |
| 5 | +=================================================== |
6 | 6 |
|
7 | 7 | In :doc:`/configuration`, you learned how to manage your application |
8 | | -configuration. At times, it may benefit your application to store certain |
9 | | -credentials outside of your project code. Database configuration is one such |
10 | | -example. The flexibility of the Symfony service container allows you to do this. |
| 8 | +configuration. In this article you'll learn how to use environment variables (or |
| 9 | +"env vars" for short) to configure some of those options, which is a common |
| 10 | +practice to configure sensitive options such as credentials and passwords. |
11 | 11 |
|
12 | 12 | .. _config-env-vars: |
13 | 13 |
|
14 | | -Environment Variables |
15 | | ---------------------- |
| 14 | +Referencing Env Vars in Configuration Files |
| 15 | +------------------------------------------- |
16 | 16 |
|
17 | | -You can reference environment variables by using special parameters named after |
18 | | -the variables you want to use enclosed between ``env()``. Their actual values |
19 | | -will be resolved at runtime (once per request), so that dumped containers can be |
20 | | -reconfigured dynamically even after being compiled. |
21 | | - |
22 | | -For example, when installing the ``doctrine`` recipe, database configuration is |
23 | | -put in a ``DATABASE_URL`` environment variable: |
| 17 | +First, define the value of the env var, using your shell environment or the |
| 18 | +``.env`` file at the project root directory. For example, consider the |
| 19 | +``DATABASE_URL`` env var defined when installing the ``doctrine`` recipe (by |
| 20 | +convention the env var names are always uppercase): |
24 | 21 |
|
25 | 22 | .. code-block:: bash |
26 | 23 |
|
27 | 24 | # .env |
28 | 25 | DATABASE_URL="mysql://db_user:db_password@127.0.0.1:3306/db_name" |
29 | 26 |
|
30 | | -This variable is referenced in the service container configuration using |
31 | | -``%env(DATABASE_URL)%``: |
| 27 | +Then, you can reference those env vars in any configuration option enclosing |
| 28 | +their names with ``env()``. Their actual values will be resolved at runtime |
| 29 | +(once per request), so that dumped containers can be reconfigured dynamically |
| 30 | +even after being compiled: |
32 | 31 |
|
33 | 32 | .. configuration-block:: |
34 | 33 |
|
@@ -69,8 +68,8 @@ This variable is referenced in the service container configuration using |
69 | 68 | ] |
70 | 69 | ]); |
71 | 70 |
|
72 | | -You can also give the ``env()`` parameters a default value: the default value |
73 | | -will be used whenever the corresponding environment variable is *not* found: |
| 71 | +You can also give the ``env()`` parameters a default value, which will be used |
| 72 | +whenever the corresponding environment variable is *not* found: |
74 | 73 |
|
75 | 74 | .. configuration-block:: |
76 | 75 |
|
@@ -101,7 +100,7 @@ will be used whenever the corresponding environment variable is *not* found: |
101 | 100 | .. _configuration-env-var-in-prod: |
102 | 101 |
|
103 | 102 | Configuring Environment Variables in Production |
104 | | -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 103 | +----------------------------------------------- |
105 | 104 |
|
106 | 105 | During development, you'll use the ``.env`` file to configure your environment |
107 | 106 | variables. On your production server, it is recommended to configure these at |
|
0 commit comments