12

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?

2
  • Have you tried to define title as an hashmap like title: {value:'someValue', subtitle:'another value'} ? By the way, parameter references have to be quoted. arguments: ['%title%'] see documentation Commented Apr 26, 2016 at 12:27
  • Possible duplicate of Symfony2: Reference string or an array in config yaml Commented Apr 26, 2016 at 12:31

2 Answers 2

16

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.

Sign up to request clarification or add additional context in comments.

Comments

15

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

2 Comments

Where did you find this syntax "@=container.getParameter('title')['subtitle']"?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.