加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 18:17:22 所属栏目:PHP教程 来源:网络整理
导读:参见英文答案 Symfony2 Use PHP Class Constant in YAML Config?6个 使用配置文件定义服务时,如何将PHP常量(本例中为CURLAUTH_DIGEST)作为构造函数参数传递? 我现在无法测试它,但我认为: services: my_service: class: "%my_service.class%" arguments: [C
参见英文答案 > Symfony2 Use PHP Class Constant in YAML Config?6个
使用配置文件定义服务时,如何将PHP常量(本例中为CURLAUTH_DIGEST)作为构造函数参数传递?

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

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

因为CURLAUTH_DIGEST被转换为字符串而无法工作.

这是一种方法

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

应用程序/配置/ config.yml

imports:
    - { resource: constants.php }

>创建一个新文件constants.php

应用程序/配置/ constants.php

<?php

$container->setParameter('curlauth.digest',CURLAUTH_DIGEST);

>您现在可以在服务中访问此常量

@包/资源/配置/ services.yml

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读