gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 10:26:09 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Symfony2 Use PHP Class Constant in YAML Config?

(6 个回答)


8年前关闭。




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

现在无法测试 但我认为:

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

无法工作,因为 CURLAUTH_DIGEST转换为 string .

最佳答案

这是一种方法来做到这一点

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

    app/config/config.yml
    imports:
    - { resource: constants.php }
  • 创建一个新文件 constants.php
    app/config/constants.php
    <?php

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

    @Bundle/Resources/config/services.yml
    services:
    my_service:
    class: "%my_service.class%"
    arguments: [%curlauth.digest%]
  • 关于symfony - 如何在 Symfony 2 中将 PHP 常量作为服务参数传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15878021/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com