如何在Symfony 2中将PHP常量作为服务参数传递? [英] How to pass a PHP constant as service argument in Symfony 2?

查看:96
本文介绍了如何在Symfony 2中将PHP常量作为服务参数传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When defining services using the configuration file, how can I pass a PHP constant (CURLAUTH_DIGEST in this example) as a constructor argument?

目前无法对其进行测试,但我认为:

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

不起作用,因为 CURLAUTH_DIGEST 转换为字符串

Wouldn't work because CURLAUTH_DIGEST is converted to a string.

推荐答案

这是一种实现方法


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

app / config / config.yml

imports:
    - { resource: constants.php }


  • 创建新文件 constants.php

    app / config / constants.php

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


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

  • You can now access this constant in your service

    @ Bundle / Resources / config / services.yml

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


  • 这篇关于如何在Symfony 2中将PHP常量作为服务参数传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆