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

    ServerName      Symfony2
    DocumentRoot    "/path/to/symfony_2_app/web"
    DirectoryIndex  index.php index.html
    SetEnv          SYMFONY__DATABASE__USER user
    SetEnv          SYMFONY__DATABASE__PASSWORD secret

    
        AllowOverride All
        Allow from All
    

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文件:





    

PHP文件:
 

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

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

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

XML文件





    
        GLOBAL_CONSTANT
        My_Class::CONSTANT_NAME
    

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



    

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');

你可能感兴趣的:(Symfony笔记)