@@ -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,84 @@ 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
+ ``env(...) `` parameters can have default values be set and used whenever their
56
+ corresponding environment variables are not found:
57
+
58
+ .. configuration-block ::
59
+
60
+ .. code-block :: yaml
61
+
62
+ parameters :
63
+ env(DATABASE_HOST) : localhost
64
+
65
+ .. code-block :: xml
66
+
67
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
68
+ <container xmlns =" http://symfony.com/schema/dic/services"
69
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
70
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd" >
71
+
72
+ <parameters >
73
+ <parameter key =" env(DATABASE_HOST)" >localhost</parameter >
74
+ </parameters >
75
+ </container >
76
+
77
+ .. code-block :: php
78
+
79
+ $container->setParameter('env(DATABASE_HOST)', 'localhost');
80
+
81
+ If you're using Apache, environment variables can be set e.g. using the following
82
+ ``VirtualHost `` configuration:
83
+
84
+ .. code-block :: apache
85
+
86
+ <VirtualHost *:80>
87
+ ServerName Symfony
88
+ DocumentRoot "/path/to/symfony_2_app/web"
89
+ DirectoryIndex index.php index.html
90
+ SetEnv DATABASE_USER user
91
+ SetEnv DATABASE_PASSWORD secret
92
+
93
+ <Directory "/path/to/symfony_2_app/web">
94
+ AllowOverride All
95
+ Allow from All
96
+ </Directory>
97
+ </VirtualHost>
98
+
99
+ .. note ::
100
+
101
+ All popular web servers support setting environment variables. Read their
102
+ documentation to know how.
103
+
104
+ Also, in order for your console to work, you must export these as shell
105
+ variables. On a Unix system, you can run the following:
106
+
107
+ .. code-block :: bash
108
+
109
+ $ export DATABASE_USER=user
110
+ $ export DATABASE_PASSWORD=secret
111
+
112
+ .. note ::
113
+
114
+ You can also define the default value of any existing parameters using
115
+ special environement variables named after their corresponding parameter
116
+ prefixed with ``SYMFONY__ `` after replacing dots by double underscores
117
+ (e.g. ``SYMFONY__FOO__BAR `` to set the default value of the ``foo.bar ``
118
+ parameter). These default values are resolved when compiling the service
119
+ container and won't change at runtime once dumped.
120
+
101
121
Constants
102
122
---------
103
123
0 commit comments