简体   繁体   中英

Symfony2: how to read parameters array in config.yml

my parameters.yml file has:

parameters:
     title:
          subtitle: value

i want to pass the value to a service in config.yml

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

how can i do this?

Symfony2 doesn't support reading individual elements on a parameter array using the % notation. What you are doing is not possible out of the box.

The only way to do that would be to create your own Symfony\\Component\\DependencyInjection\\ParameterBag\\ParameterBag which would support fetching an array item.

The % notation doesn't work but it can be accomplished the following way:

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

It works at least for symfony 2.7.3

More info about the expression language can be found in the cookbook: http://symfony.com/doc/current/book/service_container.html#using-the-expression-language

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM