@@ -13,65 +13,25 @@ you to easily do this.
13
13
Environment Variables
14
14
---------------------
15
15
16
- Symfony will grab any environment variable prefixed with ``SYMFONY__ `` and
17
- set it as a parameter in the service container. Some transformations are
18
- applied to the resulting parameter name:
16
+ .. versionadded :: 3.2
17
+ ``env(...) `` parameters were introduced in Symfony 3.2.
19
18
20
- * `` SYMFONY__ `` prefix is removed;
21
- * Parameter name is lowercased;
22
- * Double underscores are replaced with a period, as a period is not
23
- a valid character in an environment variable name .
19
+ You can reference environment variables by using special parameters named after
20
+ the variables you want to use encapsed between `` env(...) ``. Their actual values
21
+ will be resolved at runtime, so that dumped containers can be reconfigured
22
+ dynamically even after being compiled .
24
23
25
- For example, if you're using Apache, environment variables can be set using
26
- the following ``VirtualHost `` configuration:
27
-
28
- .. code-block :: apache
29
-
30
- <VirtualHost *:80>
31
- ServerName Symfony
32
- DocumentRoot "/path/to/symfony_2_app/web"
33
- DirectoryIndex index.php index.html
34
- SetEnv SYMFONY__DATABASE__USER user
35
- SetEnv SYMFONY__DATABASE__PASSWORD secret
36
-
37
- <Directory "/path/to/symfony_2_app/web">
38
- AllowOverride All
39
- Allow from All
40
- </Directory>
41
- </VirtualHost>
42
-
43
- .. note ::
44
-
45
- The example above is for an Apache configuration, using the `SetEnv `_
46
- directive. However, this will work for any web server which supports
47
- the setting of environment variables.
48
-
49
- Also, in order for your console to work (which does not use Apache),
50
- you must export these as shell variables. On a Unix system, you can run
51
- the following:
52
-
53
- .. code-block :: bash
54
-
55
- $ export SYMFONY__DATABASE__USER=user
56
- $ export SYMFONY__DATABASE__PASSWORD=secret
57
-
58
- Now that you have declared an environment variable, it will be present
59
- in the PHP ``$_SERVER `` global variable. Symfony then automatically sets all
60
- ``$_SERVER `` variables prefixed with ``SYMFONY__ `` as parameters in the service
61
- container.
62
-
63
- You can now reference these parameters wherever you need them.
24
+ For example, if you want to use the value of the ``DATABASE_HOST `` environment
25
+ variable into you service container configuration, you can reference it using
26
+ ``%env(DATABASE_HOST)% `` in your configuration files:
64
27
65
28
.. configuration-block ::
66
29
67
30
.. code-block :: yaml
68
31
69
32
doctrine :
70
33
dbal :
71
- driver : pdo_mysql
72
- dbname : symfony_project
73
- user : ' %database.user%'
74
- password : ' %database.password%'
34
+ host : ' %env(DATABASE_HOST)%'
75
35
76
36
.. code-block :: xml
77
37
@@ -80,24 +40,86 @@ You can now reference these parameters wherever you need them.
80
40
81
41
<doctrine : config >
82
42
<doctrine : dbal
83
- driver =" pdo_mysql"
84
- dbname =" symfony_project"
85
- user =" %database.user%"
86
- password =" %database.password%"
43
+ host =" %env(DATABASE_HOST)%"
87
44
/>
88
45
</doctrine : config >
89
46
90
47
.. code-block :: php
91
48
92
49
$container->loadFromExtension('doctrine', array(
93
50
'dbal' => array(
94
- 'driver' => 'pdo_mysql',
95
- 'dbname' => 'symfony_project',
96
- 'user' => '%database.user%',
97
- 'password' => '%database.password%',
51
+ 'host' => '%env(DATABASE_HOST)%',
98
52
)
99
53
));
100
54
55
+ You can also give the ``env(...) `` parameters a default value: the default value
56
+ will be used whenever the corresponding environment variable is *not * found:
57
+
58
+ .. configuration-block ::
59
+
60
+ .. code-block :: yaml
61
+
62
+ # app/config/parameters.yml
63
+
64
+ parameters :
65
+ env(DATABASE_HOST) : localhost
66
+
67
+ .. code-block :: xml
68
+
69
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
70
+ <container xmlns =" http://symfony.com/schema/dic/services"
71
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
72
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd" >
73
+
74
+ <parameters >
75
+ <parameter key =" env(DATABASE_HOST)" >localhost</parameter >
76
+ </parameters >
77
+ </container >
78
+
79
+ .. code-block :: php
80
+
81
+ $container->setParameter('env(DATABASE_HOST)', 'localhost');
82
+
83
+ If you're using Apache, environment variables can be set e.g. using the following
84
+ ``VirtualHost `` configuration:
85
+
86
+ .. code-block :: apache
87
+
88
+ <VirtualHost *:80>
89
+ ServerName Symfony
90
+ DocumentRoot "/path/to/symfony_2_app/web"
91
+ DirectoryIndex index.php index.html
92
+ SetEnv DATABASE_USER user
93
+ SetEnv DATABASE_PASSWORD secret
94
+
95
+ <Directory "/path/to/symfony_2_app/web">
96
+ AllowOverride All
97
+ Allow from All
98
+ </Directory>
99
+ </VirtualHost>
100
+
101
+ .. note ::
102
+
103
+ All popular web servers support setting environment variables. Read their
104
+ documentation to find out how.
105
+
106
+ Also, in order for your console to work, you must export these as shell
107
+ variables. On a Unix system, you can run the following:
108
+
109
+ .. code-block :: bash
110
+
111
+ $ export DATABASE_USER=user
112
+ $ export DATABASE_PASSWORD=secret
113
+
114
+ .. note ::
115
+
116
+ You can also define the default value of any existing parameters using
117
+ special environment variables named after their corresponding parameter
118
+ prefixed with ``SYMFONY__ `` after replacing dots by double underscores
119
+ (e.g. ``SYMFONY__KERNEL__CHARSET `` to set the default value of the
120
+ ``kernel.charset `` parameter). These default values are resolved when
121
+ compiling the service container and won't change at runtime once dumped.
122
+
101
123
Constants
102
124
---------
103
125
0 commit comments