Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 007214a

Browse filesBrowse files
committed
feature #6918 [DependencyInjection] Document %env(...)% dynamic parameters (nicolas-grekas)
This PR was merged into the 3.2 branch. Discussion ---------- [DependencyInjection] Document %env(...)% dynamic parameters Ref. symfony/symfony#19681 Commits ------- 083da46 [DI] Document %env()% dynamic parameters
2 parents b9f126c + 083da46 commit 007214a
Copy full SHA for 007214a

File tree

1 file changed

+81
-86
lines changed
Filter options

1 file changed

+81
-86
lines changed

‎configuration/external_parameters.rst

Copy file name to clipboardExpand all lines: configuration/external_parameters.rst
+81-86Lines changed: 81 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -13,119 +13,114 @@ you to easily do this.
1313
Environment Variables
1414
---------------------
1515

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.
7018

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.
8223

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:
9227

9328
.. configuration-block::
9429

9530
.. code-block:: yaml
9631
32+
# app/config/config.yml
9733
doctrine:
9834
dbal:
99-
driver: pdo_mysql
100-
dbname: symfony_project
101-
user: '%database.user%'
102-
password: '%database.password%'
35+
host: '%env(DATABASE_HOST)%'
10336
10437
.. code-block:: xml
10538
39+
<!-- app/config/config.xml -->
10640
<!-- xmlns:doctrine="http://symfony.com/schema/dic/doctrine" -->
10741
<!-- xsi:schemaLocation="http://symfony.com/schema/dic/doctrine http://symfony.com/schema/dic/doctrine/doctrine-1.0.xsd"> -->
10842
10943
<doctrine:config>
11044
<doctrine:dbal
111-
driver="pdo_mysql"
112-
dbname="symfony_project"
113-
user="%database.user%"
114-
password="%database.password%"
45+
host="%env(DATABASE_HOST)%"
11546
/>
11647
</doctrine:config>
11748
11849
.. code-block:: php
11950
51+
// app/config/config.php
12052
$container->loadFromExtension('doctrine', array(
12153
'dbal' => array(
122-
'driver' => 'pdo_mysql',
123-
'dbname' => 'symfony_project',
124-
'user' => '%database.user%',
125-
'password' => '%database.password%',
54+
'host' => '%env(DATABASE_HOST)%',
12655
)
12756
));
12857
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+
129124
Constants
130125
---------
131126

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.