Home > OS >  Symfony: Use a value within a parameter array as a service parameter
Symfony: Use a value within a parameter array as a service parameter

Time:09-22

Symfony 3.4

I have a parameter that looks like this:

parameters.yml:

paramters:
    some_configuration:
        param1: value1
        param2: value2

I want to pass a value within my some_configuration parameter directly to a service, rather that some_configuration itself. Something like this:

services.yml:

services:
    AppBundle\Service\MyService:
        arguments:
            $serviceparam: '%some_configuration.param2%'

Is this possible somehow?

CodePudding user response:

services:
    AppBundle\Service\MyService:
        arguments:
            $serviceparam: '@=parameter("some_configuration")["param2"]'

CodePudding user response:

Above Symfony 2.7.3

In all the Symfony config files I've seen, the entries under 'parameters:' have always been fully qualified.
I don't fully understand why this is but it may help you to write the entries in your parameters.yml like this:

parameters.yml:

parameters:
    some_configuration.param1: value1
    some_configuration.param2: value2

To use parameter array values in service.yaml you can use it as a normal parameter (like what you've written!):

service.yaml

services:
    AppBundle\Service\MyService:
        arguments:
            $serviceparam: '%some_configuration.param2%'

In symfony 2.7.3:

It can be accomplished the following way:

services:
    AppBundle\Service\MyService:
        arguments:
            $serviceparam: ["@=container.getParameter('some_configuration')['param1']"]

It works for symfony 2.7.3
More info about the expression language can be found in the symfony doc

  • Related