Symfony2 在 YAML 配置中使用 PHP 类常量?

2022-01-14 00:00:00 yaml php symfony

我知道这可能是不可能的,但是有没有一种干净的方法可以在 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.

相关文章