@@ -10,20 +10,57 @@ to store certain credentials outside of your project code. Database configuratio
1010is one such example. The flexibility of the Symfony service container allows
1111you to easily do this.
1212
13- Environment Variables
14- ---------------------
13+ Environment Variables as Default Parameter Values
14+ -------------------------------------------------
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+ Any parameters used in the service container can have their default values be set
17+ using environment variables prefixed with `` SYMFONY__ `` and where dots in
18+ parameter names are replaced by double underscores.
1919
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.
20+ For example, if you have a configuration that reads as such:
2421
25- For example, if you're using Apache, environment variables can be set using
26- the following ``VirtualHost `` configuration:
22+ .. configuration-block ::
23+
24+ .. code-block :: yaml
25+
26+ doctrine :
27+ dbal :
28+ driver : pdo_mysql
29+ dbname : symfony_project
30+ user : ' %database.user%'
31+ password : ' %database.password%'
32+
33+ .. code-block :: xml
34+
35+ <!-- xmlns:doctrine="http://symfony.com/schema/dic/doctrine" -->
36+ <!-- xsi:schemaLocation="http://symfony.com/schema/dic/doctrine http://symfony.com/schema/dic/doctrine/doctrine-1.0.xsd"> -->
37+
38+ <doctrine : config >
39+ <doctrine : dbal
40+ driver =" pdo_mysql"
41+ dbname =" symfony_project"
42+ user =" %database.user%"
43+ password =" %database.password%"
44+ />
45+ </doctrine : config >
46+
47+ .. code-block :: php
48+
49+ $container->loadFromExtension('doctrine', array(
50+ 'dbal' => array(
51+ 'driver' => 'pdo_mysql',
52+ 'dbname' => 'symfony_project',
53+ 'user' => '%database.user%',
54+ 'password' => '%database.password%',
55+ )
56+ ));
57+
58+ Then you can set the default value of the `database.user ` and `database.password `
59+ parameters by setting the `SYMFONY__DATABASE__USER ` and
60+ `SYMFONY__DATABASE__PASSWORD ` environment variables respectively.
61+
62+ If you're using Apache, those can be set using the following ``VirtualHost ``
63+ configuration:
2764
2865.. code-block :: apache
2966
@@ -55,23 +92,29 @@ the following ``VirtualHost`` configuration:
5592 $ export SYMFONY__DATABASE__USER=user
5693 $ export SYMFONY__DATABASE__PASSWORD=secret
5794
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 .
95+ `` SYMFONY__ `` prefixed environment variables are ignored when their corresponding
96+ parameters are set in your configuration files. Note also that their values are
97+ inlined into dumped service containers so that changing their values at runtime
98+ won't affect these containers unless you recompile them afterwards .
6299
63- You can now reference these parameters wherever you need them.
100+ Reference Environment Variables at Run-time
101+ -------------------------------------------
102+
103+ You can reference any environment variables for it to be used in your service
104+ container by using special parameters named after the variables you want to use
105+ encapsed in between `%env(...)% `.
106+
107+ For example, if you want to use the value of the `DATABASE_HOST ` environment
108+ variable into you service container configuration, you can reference it using the
109+ `%env(DATABASE_HOST)% ` parameter:
64110
65111.. configuration-block ::
66112
67113 .. code-block :: yaml
68114
69115 doctrine :
70116 dbal :
71- driver : pdo_mysql
72- dbname : symfony_project
73- user : ' %database.user%'
74- password : ' %database.password%'
117+ host : ' %env(DATABASE_HOST)%'
75118
76119 .. code-block :: xml
77120
@@ -80,24 +123,48 @@ You can now reference these parameters wherever you need them.
80123
81124 <doctrine : config >
82125 <doctrine : dbal
83- driver =" pdo_mysql"
84- dbname =" symfony_project"
85- user =" %database.user%"
86- password =" %database.password%"
126+ host =" %env(DATABASE_HOST)%"
87127 />
88128 </doctrine : config >
89129
90130 .. code-block :: php
91131
92132 $container->loadFromExtension('doctrine', array(
93133 'dbal' => array(
94- 'driver' => 'pdo_mysql',
95- 'dbname' => 'symfony_project',
96- 'user' => '%database.user%',
97- 'password' => '%database.password%',
134+ 'host' => '%env(DATABASE_HOST)%',
98135 )
99136 ));
100137
138+ `%env(...)% ` parameters can have default values be set that are going to be used
139+ whenever their corresponding environment variables are not found:
140+
141+ .. configuration-block ::
142+
143+ .. code-block :: yaml
144+
145+ parameters :
146+ env(DATABASE_HOST) : localhost
147+
148+ .. code-block :: xml
149+
150+ <?xml version =" 1.0" encoding =" UTF-8" ?>
151+ <container xmlns =" http://symfony.com/schema/dic/services"
152+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
153+ xsi : schemaLocation =" http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd" >
154+
155+ <parameters >
156+ <parameter key =" env(DATABASE_HOST)" >localhost</parameter >
157+ </parameters >
158+ </container >
159+
160+ .. code-block :: php
161+
162+ $container->setParameter('env(DATABASE_HOST)', 'localhost');
163+
164+ Unlike ``SYMFONY__ `` prefixed environment variables, `%env(...)% ` parameters are
165+ truned into run-time environment variable reads, so that a dumped container can
166+ be reconfigured dynamically even after being compiled and dumped.
167+
101168Constants
102169---------
103170
0 commit comments