如何在Symfony 2中将PHP常量作为服务参数传递?

gre*_*emo 7 yaml constants symfony

使用配置文件定义服务时,如何将PHP常量(CURLAUTH_DIGEST在此示例中)作为构造函数参数传递?

现在无法测试它,但我认为:

services:
    my_service:
        class: "%my_service.class%"
        arguments: [CURLAUTH_DIGEST]
Run Code Online (Sandbox Code Playgroud)

因为CURLAUTH_DIGEST转换为a 而无法工作string.

Tou*_*uki 13

这是一种方法

  1. 在配置中添加一行以包含.php配置

    app/config/config.yml

    imports:
        - { resource: constants.php }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 创建一个新文件 constants.php

    app/config/constants.php

    <?php
    
    $container->setParameter('curlauth.digest', CURLAUTH_DIGEST);
    
    Run Code Online (Sandbox Code Playgroud)
  3. 您现在可以在服务中访问此常量

    @Bundle/Resources/config/services.yml

    services:
        my_service:
            class: "%my_service.class%"
            arguments: [%curlauth.digest%]
    
    Run Code Online (Sandbox Code Playgroud)