@@ -13,93 +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:
19
-
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.
24
-
25
- For example, if you're using Apache, environment variables can be set using the
26
- `SetEnv `_ directive with 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
- For Nginx web servers, the environment variables can be set with the `fastcgi_param `_
44
- directive. For example, in the configuration file where the ``fastcgi_params ``
45
- file is included:
46
-
47
- .. code-block :: nginx
48
-
49
- server {
50
- server_name domain.tld www.domain.tld;
51
- root /var/www/project/web;
52
-
53
- location / {
54
- try_files $uri /app.php$is_args$args;
55
- }
56
-
57
- location ~ ^/app\.php(/|$) {
58
- fastcgi_pass unix:/var/run/php5-fpm.sock;
59
- fastcgi_split_path_info ^(.+\.php)(/.*)$;
60
- include fastcgi_params;
61
- fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
62
- fastcgi_param DOCUMENT_ROOT $realpath_root;
63
- fastcgi_param SYMFONY__DATABASE__USER user;
64
- fastcgi_param SYMFONY__DATABASE__PASSWORD secret;
65
- internal;
66
- }
67
-
68
- # ...
69
- }
16
+ .. versionadded :: 3.2
17
+ ``env() `` parameters were introduced in Symfony 3.2.
70
18
71
- .. note ::
72
-
73
- The examples above are for an Apache and Nginx configuration. However, this
74
- will work for any web server which supports the setting of environment
75
- variables.
76
-
77
- Also, in order for your console to work (which does not use a web server),
78
- you must export these as shell variables. On a Unix system, you can run
79
- the following:
80
-
81
- .. code-block :: terminal
19
+ You can reference environment variables by using special parameters named after
20
+ the variables you want to use enclosed between ``env() ``. Their actual values
21
+ will be resolved at runtime, so that dumped containers can be reconfigured
22
+ dynamically even after being compiled.
82
23
83
- $ export SYMFONY__DATABASE__USER=user
84
- $ export SYMFONY__DATABASE__PASSWORD=secret
85
-
86
- Now that you have declared an environment variable, it will be present
87
- in the PHP ``$_SERVER `` global variable. Symfony then automatically sets all
88
- ``$_SERVER `` variables prefixed with ``SYMFONY__ `` as parameters in the service
89
- container.
90
-
91
- 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 in you service container configuration, you can reference it using
26
+ ``%env(DATABASE_HOST)% `` in your configuration files:
92
27
93
28
.. configuration-block ::
94
29
95
30
.. code-block :: yaml
96
31
97
32
doctrine :
98
33
dbal :
99
- driver : pdo_mysql
100
- dbname : symfony_project
101
- user : ' %database.user%'
102
- password : ' %database.password%'
34
+ host : ' %env(DATABASE_HOST)%'
103
35
104
36
.. code-block :: xml
105
37
@@ -108,24 +40,82 @@ You can now reference these parameters wherever you need them.
108
40
109
41
<doctrine : config >
110
42
<doctrine : dbal
111
- driver =" pdo_mysql"
112
- dbname =" symfony_project"
113
- user =" %database.user%"
114
- password =" %database.password%"
43
+ host =" %env(DATABASE_HOST)%"
115
44
/>
116
45
</doctrine : config >
117
46
118
47
.. code-block :: php
119
48
120
49
$container->loadFromExtension('doctrine', array(
121
50
'dbal' => array(
122
- 'driver' => 'pdo_mysql',
123
- 'dbname' => 'symfony_project',
124
- 'user' => '%database.user%',
125
- 'password' => '%database.password%',
51
+ 'host' => '%env(DATABASE_HOST)%',
126
52
)
127
53
));
128
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
+ parameters :
64
+ database_host : ' %env(DATABASE_HOST)%'
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 =" database_host" >%env(DATABASE_HOST)%</parameter >
76
+ <parameter key =" env(DATABASE_HOST)" >localhost</parameter >
77
+ </parameters >
78
+ </container >
79
+
80
+ .. code-block :: php
81
+
82
+ $container->setParameter('database_host', '%env(DATABASE_HOST)%');
83
+ $container->setParameter('env(DATABASE_HOST)', 'localhost');
84
+
85
+ Setting environment variables is generally done at the web server level or in the
86
+ terminal. If you're using Apache, Nginx or just the console, you can use e.g. one
87
+ of the following:
88
+
89
+ .. configuration-block ::
90
+
91
+ .. code-block :: apache
92
+
93
+ <VirtualHost *:80>
94
+ # ...
95
+
96
+ SetEnv DATABASE_USER user
97
+ SetEnv DATABASE_PASSWORD secret
98
+ </VirtualHost>
99
+
100
+ .. code-block :: nginx
101
+
102
+ fastcgi_param DATABASE_USER user
103
+ fastcgi_param DATABASE_PASSWORD secret
104
+
105
+ .. code-block :: terminal
106
+
107
+ $ export DATABASE_USER=user
108
+ $ export DATABASE_PASSWORD=secret
109
+
110
+ .. tip ::
111
+
112
+ You can also define the default value of any existing parameters using
113
+ special environment variables named after their corresponding parameter
114
+ prefixed with ``SYMFONY__ `` after replacing dots by double underscores
115
+ (e.g. ``SYMFONY__KERNEL__CHARSET `` to set the default value of the
116
+ ``kernel.charset `` parameter). These default values are resolved when
117
+ compiling the service container and won't change at runtime once dumped.
118
+
129
119
Constants
130
120
---------
131
121
0 commit comments