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

Commit3021c4f

Browse files
[DI] Document %env(...)% dynamic parameters
1 parent626ab4f commit3021c4f

File tree

1 file changed

+78
-58
lines changed

1 file changed

+78
-58
lines changed

‎configuration/external_parameters.rst‎

Lines changed: 78 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::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:
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,84 @@ 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+
``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+
<containerxmlns="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+
<parameterkey="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+
101121
Constants
102122
---------
103123

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp