symfony配置文件之一:parameters和环境变量

目录

 

1、设置parameters关键字变量(在配置文件中)

2.env文件和环境变量(在.env文件中)

3.在外部设置服务容器变量

(1)设置环境变量(在apache中)

(2)设置常量(在配置文件中)

(3)组合导入设置(在配置文件中)

4、在控制器中如何引用parameters参数


1、设置parameters关键字变量(在配置文件中)

在symfony配置文件中,我们使用parameters关键字来定义一些变量,这些变量可以在配置文件的其它地方进行引用。

For example, when you install the translation package, a locale parameter is added to config/services.yaml:

# config/services.yaml
parameters:
    locale: en
# ...

This parameter is then referenced in the framework config in config/packages/translation.yaml:

# config/packages/translation.yaml
framework:
    # any string surrounded by two % is replaced by that parameter value
    default_locale: '%locale%'
# ...


You can define whatever parameter names you want under the parameters key of any configuration file. To reference a parameter, surround its name with two percent signs - e.g. %locale%.

2.env文件和环境变量(在.env文件中)

.env文件位于应用的根目录下,该文件将被symfony系统自动加载,它的内容将被设置为环境变量。这个用法在开发环境中,或者在部署时很难设置环境变量的情况下,将非常有用。

For example, if you install the doctrine package, then you will have an environment variable called
DATABASE_URL in your .env file. This is referenced inside config/packages/doctrine.yaml:

在配置文件中,使用'%env(resolve:DATABASE_URL)%'来引用。

# config/packages/doctrine.yaml
doctrine:
    dbal:
        url: '%env(DATABASE_URL)%'
        # The `resolve:` prefix replaces container params by their values inside the env variable:
        # url: '%env(resolve:DATABASE_URL)%'

3.在外部设置服务容器变量

(1)设置环境变量(在apache中)

Symfony会自动获取以SYMFONY__为前缀的环境变量,然后将它的前缀去除且以__为分割保存为数组参数。如在apache中设置了一个变量SYMFONY__DATABASE__USER,symfony将获取并保存一个database.user变量。

for example, if you're using Apache, environment variables can be set using the following VirtualHost configuration:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<VirtualHost *:80>
    ServerName      Symfony2
    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>

Note

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:

1
2
$ 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.

现在你将在配置文件的任何地方通过%%来访问,实例如下

YAML文件:

doctrine:
    dbal:
        driver    pdo_mysql
        dbname:   symfony2_project
        user:     "%database.user%"
        password: "%database.password%"

XML文件:

<!-- 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="symfony2_project"
        user="%database.user%"
        password="%database.password%"
    />
</doctrine:config>

PHP文件:
 

$container->loadFromExtension('doctrine', array(
    'dbal' => array(
        'driver'   => 'pdo_mysql',
        'dbname'   => 'symfony2_project',
        'user'     => '%database.user%',
        'password' => '%database.password%',
    )
));

(2)设置常量(在配置文件中)

服务容器还支持设置常量作为参数,如下所示:

XML文件

<?xml version="1.0" encoding="UTF-8"?>

<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <parameters>
        <parameter key="global.constant.value" type="constant">GLOBAL_CONSTANT</parameter>
        <parameter key="my_class.constant.value" type="constant">My_Class::CONSTANT_NAME</parameter>
    </parameters>
</container>

PHP文件

$container->setParameter('global.constant.value', GLOBAL_CONSTANT);
$container->setParameter('my_class.constant.value', My_Class::CONSTANT_NAME);

提示:但这种方法仅限于xml文件和php文件形式的定义,如你正在使用yaml配置,可以导入一个xml文件。

YAML文件

# app/config/config.yml
imports:
    - { resource: parameters.xml }

(3)组合导入设置(在配置文件中)

使用imports:键导入其它任何地方定义的参数parameters.php文件(支持PHP, XML, YAML, INI, and closure resources文件)。

YAML

# app/config/config.yml
imports:
    - { resource: parameters.php }

XML

<!-- app/config/config.xml -->
<imports>
    <import resource="parameters.php" />
</imports>

PHP

// app/config/config.php
$loader->import('parameters.php');

In parameters.php, tell the service container the parameters that you wish to set. This is useful when important configuration is in a nonstandard format. The example below includes a Drupal database's configuration in the Symfony service container.

1
2
3
// app/config/parameters.php
include_once('/path/to/drupal/sites/default/settings.php');
$container->setParameter('drupal.database.url', $db_url);

 

4、在控制器中如何引用parameters参数

app/config/parameters.yml.

parameters:
    api_pass: apipass
    api_user: apiuser

In Symfony 2.6 and older versions, to get a parameter in a controller - you should get the container first, and then - the needed parameter.

$this->container->getParameter('api_user');
This documentation chapter explains it.

While $this->get() method in a controller will load a service (doc)

In Symfony 2.7 and newer versions, to get a parameter in a controller you can use the following:

$this->getParameter('api_user');

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值