Symfony2在YAML配置中使用PHP类常量? [英] Symfony2 Use PHP Class Constant in YAML Config?

查看:119
本文介绍了Symfony2在YAML配置中使用PHP类常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这可能是不可能的,但是有一种干净的方法可以在YAML config/services/etc中使用PHP类常量.是Symfony2的文件吗?

I know this is probably not possible, but is there a clean way to use a PHP class constant within a YAML config/services/etc. file for Symfony2?

例如,如果我有这个:

namespace My\Bundle\DependencyInjection;

class MyClass
{
    const MY_CONST = 'cookies';
}

是否可能这样(在.yml文件中):

Is something like this possible (in a .yml file):

services:
    my_service:
        class: Some\Class
        arguments:
            - %My\Bundle\DependencyInjection\MyClass::MY_CONST%

这将有助于保持两者之间的一致性.

That'd go a long way in helping maintain consistency between the two.

推荐答案

在Symfony 3.2之前的版本中,注入PHP常量仅适用于XML:

In versions before Symfony 3.2, injecting PHP-constants only works with XML:

<parameter key="my_service.my_const" type="constant">My\Bundle\DependencyInjection\MyClass::MY_CONST</parameter>

如果要保留yml文件,可以将xml文件导入到services.yml中.混合配置样式可能有点难看,但是据我所知,这是唯一的方法.

If you want to keep yor yml files, you could just import the xml-file into your services.yml. Mixing config styles might be a bit ugly, but as far as I know this is the only way to do it.

如果这对您不起作用,则适用于我对您的问题的评论.

If this doesn't work for you, my comment to your question applies.

这篇关于Symfony2在YAML配置中使用PHP类常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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