Symfony2:如何在config.yml中读取参数数组

时间:2013-11-12 02:15:12

标签: php symfony yaml

我的parameters.yml文件有:

parameters:
     title:
          subtitle: value

我想将value传递给config.yml

中的服务
my_service:
        class: the_class
        arguments: [ %title.subtitle%] //didn't work
        arguments: [ %title['subtitle']%] //didn't work

我该怎么做?

2 个答案:

答案 0 :(得分:16)

Symfony2不支持使用%表示法读取参数数组上的各个元素。你开始做的事情不可能开箱即用。

唯一的方法是创建自己的Symfony\Component\DependencyInjection\ParameterBag\ParameterBag,这将支持获取数组项。

答案 1 :(得分:14)

%符号不起作用,但可以通过以下方式完成:

my_service:
    class: the_class
    arguments: ["@=container.getParameter('title')['subtitle']"]

它至少适用于symfony 2.7.3

有关表达语言的更多信息可以在cookbook中找到: http://symfony.com/doc/current/book/service_container.html#using-the-expression-language

相关问题