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

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

问题描述

我知道这可能是不可能的,但是有没有一种干净的方法可以在 YAML 配置/服务/等中使用 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 MyBundleDependencyInjection;

class MyClass
{
    const MY_CONST = 'cookies';
}

这样的事情可能吗(在 .yml 文件中):

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

services:
    my_service:
        class: SomeClass
        arguments:
            - %MyBundleDependencyInjectionMyClass::MY_CONST%

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

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

推荐答案

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

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

<parameter key="my_service.my_const" type="constant">MyBundleDependencyInjectionMyClass::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天全站免登陆