PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, January 10, 2022

[FIXED] Symfony: Use a value within a parameter array as a service parameter

 January 10, 2022     symfony, symfony-3.4, symfony-dependency-injection     No comments   

Issue

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?


Solution

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



Answered By - Amir Shamsi
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing