使用配置文件定义服务时,如何将 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%]