Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit27399c0

Browse files
[DI] Document %env(...)% dynamic parameters
1 parent2b027a9 commit27399c0

File tree

1 file changed

+80
-58
lines changed

1 file changed

+80
-58
lines changed

‎configuration/external_parameters.rst‎

Lines changed: 80 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -13,65 +13,25 @@ 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:
16+
..versionadded::3.2
17+
``env(...)`` parameters were introduced in Symfony 3.2.
1918

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

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::terminal
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:
6427

6528
..configuration-block::
6629

6730
..code-block::yaml
6831
6932
doctrine:
7033
dbal:
71-
driver:pdo_mysql
72-
dbname:symfony_project
73-
user:'%database.user%'
74-
password:'%database.password%'
34+
host:'%env(DATABASE_HOST)%'
7535
7636
..code-block::xml
7737
@@ -80,24 +40,86 @@ You can now reference these parameters wherever you need them.
8040
8141
<doctrine:config>
8242
<doctrine:dbal
83-
driver="pdo_mysql"
84-
dbname="symfony_project"
85-
user="%database.user%"
86-
password="%database.password%"
43+
host="%env(DATABASE_HOST)%"
8744
/>
8845
</doctrine:config>
8946
9047
..code-block::php
9148
9249
$container->loadFromExtension('doctrine', array(
9350
'dbal' => array(
94-
'driver' => 'pdo_mysql',
95-
'dbname' => 'symfony_project',
96-
'user' => '%database.user%',
97-
'password' => '%database.password%',
51+
'host' => '%env(DATABASE_HOST)%',
9852
)
9953
));
10054
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+
<containerxmlns="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+
<parameterkey="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::terminal
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+
101123
Constants
102124
---------
103125

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp