【问题标题】:How to pass a PHP constant as service argument in Symfony 2? [duplicate]如何在 Symfony 2 中将 PHP 常量作为服务参数传递? [复制]
【发布时间】:2013-03-30 11:18:35
【问题描述】:

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

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

services:
    my_service:
        class: "%my_service.class%"
        arguments: [CURLAUTH_DIGEST]

不起作用,因为 CURLAUTH_DIGEST 已转换为 string

【问题讨论】:

    标签: symfony constants yaml


    【解决方案1】:

    这是一种方法

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

      app/config/config.yml

      imports:
          - { resource: constants.php }
      
    2. 创建一个新文件constants.php

      app/config/constants.php

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

      @Bundle/Resources/config/services.yml

      services:
          my_service:
              class: "%my_service.class%"
              arguments: [%curlauth.digest%]
      

    【讨论】:

    • 谢谢,这向我们展示了如何在 YAML 中进行操作。重复的答案(我读得不是很好)指出不可能这样做(仅在 XML 中)。我投票决定关闭,现在我会重新开放。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-14
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    • 2015-11-08
    相关资源
    最近更新 更多