@@ -13,119 +13,114 @@ 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 (once per request), so that dumped containers can be
22
+ reconfigured 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
32
+ # app/config/config.yml
97
33
doctrine :
98
34
dbal :
99
- driver : pdo_mysql
100
- dbname : symfony_project
101
- user : ' %database.user%'
102
- password : ' %database.password%'
35
+ host : ' %env(DATABASE_HOST)%'
103
36
104
37
.. code-block :: xml
105
38
39
+ <!-- app/config/config.xml -->
106
40
<!-- xmlns:doctrine="http://symfony.com/schema/dic/doctrine" -->
107
41
<!-- xsi:schemaLocation="http://symfony.com/schema/dic/doctrine http://symfony.com/schema/dic/doctrine/doctrine-1.0.xsd"> -->
108
42
109
43
<doctrine : config >
110
44
<doctrine : dbal
111
- driver =" pdo_mysql"
112
- dbname =" symfony_project"
113
- user =" %database.user%"
114
- password =" %database.password%"
45
+ host =" %env(DATABASE_HOST)%"
115
46
/>
116
47
</doctrine : config >
117
48
118
49
.. code-block :: php
119
50
51
+ // app/config/config.php
120
52
$container->loadFromExtension('doctrine', array(
121
53
'dbal' => array(
122
- 'driver' => 'pdo_mysql',
123
- 'dbname' => 'symfony_project',
124
- 'user' => '%database.user%',
125
- 'password' => '%database.password%',
54
+ 'host' => '%env(DATABASE_HOST)%',
126
55
)
127
56
));
128
57
58
+ You can also give the ``env() `` parameters a default value: the default value
59
+ will be used whenever the corresponding environment variable is *not * found:
60
+
61
+ .. configuration-block ::
62
+
63
+ .. code-block :: yaml
64
+
65
+ # app/config/parameters.yml
66
+ parameters :
67
+ database_host : ' %env(DATABASE_HOST)%'
68
+ env(DATABASE_HOST) : localhost
69
+
70
+ .. code-block :: xml
71
+
72
+ <!-- app/config/parameters.xml -->
73
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
74
+ <container xmlns =" http://symfony.com/schema/dic/services"
75
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
76
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd" >
77
+
78
+ <parameters >
79
+ <parameter key =" database_host" >%env(DATABASE_HOST)%</parameter >
80
+ <parameter key =" env(DATABASE_HOST)" >localhost</parameter >
81
+ </parameters >
82
+ </container >
83
+
84
+ .. code-block :: php
85
+
86
+ // app/config/parameters.php
87
+ $container->setParameter('database_host', '%env(DATABASE_HOST)%');
88
+ $container->setParameter('env(DATABASE_HOST)', 'localhost');
89
+
90
+ Setting environment variables is generally done at the web server level or in the
91
+ terminal. If you're using Apache, Nginx or just the console, you can use e.g. one
92
+ of the following:
93
+
94
+ .. configuration-block ::
95
+
96
+ .. code-block :: apache
97
+
98
+ <VirtualHost *:80>
99
+ # ...
100
+
101
+ SetEnv DATABASE_USER user
102
+ SetEnv DATABASE_PASSWORD secret
103
+ </VirtualHost>
104
+
105
+ .. code-block :: nginx
106
+
107
+ fastcgi_param DATABASE_USER user
108
+ fastcgi_param DATABASE_PASSWORD secret
109
+
110
+ .. code-block :: terminal
111
+
112
+ $ export DATABASE_USER=user
113
+ $ export DATABASE_PASSWORD=secret
114
+
115
+ .. tip ::
116
+
117
+ You can also define the default value of any existing parameters using
118
+ special environment variables named after their corresponding parameter
119
+ prefixed with ``SYMFONY__ `` after replacing dots by double underscores
120
+ (e.g. ``SYMFONY__KERNEL__CHARSET `` to set the default value of the
121
+ ``kernel.charset `` parameter). These default values are resolved when
122
+ compiling the service container and won't change at runtime once dumped.
123
+
129
124
Constants
130
125
---------
131
126
0 commit comments