发布于 2015-08-27 16:41:38 | 159 次阅读 | 评论: 0 | 来源: 网络整理
In the chapter 如何掌握和创建新的运行环境, you learned how to manage your application configuration. At times, it may benefit your application to store certain credentials outside of your project code. Database configuration is one such example. The flexibility of the Symfony service container allows you to easily do this.
Symfony will grab any environment variable prefixed with SYMFONY__
and
set it as a parameter in the service container. Some transformations are
applied to the resulting parameter name:
SYMFONY__
prefix is removed;For example, if you’re using Apache, environment variables can be set using
the following VirtualHost
configuration:
<VirtualHost *:80>
ServerName Symfony
DocumentRoot "/path/to/symfony_2_app/web"
DirectoryIndex index.php index.html
SetEnv SYMFONY__DATABASE__USER user
SetEnv SYMFONY__DATABASE__PASSWORD secret
<Directory "/path/to/symfony_2_app/web">
AllowOverride All
Allow from All
</Directory>
</VirtualHost>
注解
The example above is for an Apache configuration, using the SetEnv directive. However, this will work for any web server which supports the setting of environment variables.
Also, in order for your console to work (which does not use Apache), you must export these as shell variables. On a Unix system, you can run the following:
$ export SYMFONY__DATABASE__USER=user
$ export SYMFONY__DATABASE__PASSWORD=secret
Now that you have declared an environment variable, it will be present
in the PHP $_SERVER
global variable. Symfony then automatically sets all
$_SERVER
variables prefixed with SYMFONY__
as parameters in the service
container.
You can now reference these parameters wherever you need them.
doctrine:
dbal:
driver pdo_mysql
dbname: symfony_project
user: "%database.user%"
password: "%database.password%"
<!-- xmlns:doctrine="http://symfony.com/schema/dic/doctrine" -->
<!-- xsi:schemaLocation="http://symfony.com/schema/dic/doctrine http://symfony.com/schema/dic/doctrine/doctrine-1.0.xsd"> -->
<doctrine:config>
<doctrine:dbal
driver="pdo_mysql"
dbname="symfony_project"
user="%database.user%"
password="%database.password%"
/>
</doctrine:config>
$container->loadFromExtension('doctrine', array(
'dbal' => array(
'driver' => 'pdo_mysql',
'dbname' => 'symfony_project',
'user' => '%database.user%',
'password' => '%database.password%',
)
));
The container also has support for setting PHP constants as parameters. See Constants as Parameters for more details.
The imports
directive can be used to pull in parameters stored elsewhere.
Importing a PHP file gives you the flexibility to add whatever is needed
in the container. The following imports a file named parameters.php
.
# app/config/config.yml
imports:
- { resource: parameters.php }
<!-- app/config/config.xml -->
<imports>
<import resource="parameters.php" />
</imports>
// app/config/config.php
$loader->import('parameters.php');
注解
A resource file can be one of many types. PHP, XML, YAML, INI, and
closure resources are all supported by the imports
directive.
In parameters.php
, tell the service container the parameters that you wish
to set. This is useful when important configuration is in a non-standard
format. The example below includes a Drupal database configuration in
the Symfony service container.
// app/config/parameters.php
include_once('/path/to/drupal/sites/default/settings.php');
$container->setParameter('drupal.database.url', $db_url);