本文介绍了如何在 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 已转换为 string.

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

推荐答案

这是一种方法

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

imports:
    - { resource: constants.php }

  • 新建文件constants.php

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

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

  • You can now access this constant in your service

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

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

    08-04 11:35
    查看更多