Symfony2: How to inject ALL parameters in a service? Symfony2: How to inject ALL parameters in a service? symfony symfony

Symfony2: How to inject ALL parameters in a service?


It is not a good practice to inject the entire Container into a service. Also if you have many parameters that you need for your service it is not nice to inject all of them one by one to your service. Instead I use this method:

1) In config.yml I define the parameters that I need for my service like this:

 parameters:    product.shoppingServiceParams:        parameter1: 'Some data'        parameter2: 'some data'        parameter3: 'some data'        parameter4: 'some data'        parameter5: 'some data'        parameter6: 'some data'

2) Then I inject this root parameter to my service like:

services:  product.shoppingService:    class: Saman\ProductBundle\Service\Shopping    arguments: [@translator.default, %product.shoppingServiceParams%]

3) In may service I can access these parameters like:

namespace Saman\ProductBundle\Service;use Symfony\Bundle\FrameworkBundle\Translation\Translator;class Shopping{       protected $translator;    protected $parameters;    public function __construct(        Translator $translator,         $parameters        )     {        $this->translator = $translator;        $this->parameters = $parameters;    }    public function dummyFunction()    {        var_dump($this->getParameter('parameter2'));    }    private function getParameter($key, $default = null)    {        if (isset($this->parameters[$key])) {            return $this->parameters[$key];        }        return $default;    }  }

4) I can also set different values for different environments. For example in config_dev.yml

 parameters:    product.shoppingServiceParams:        parameter1: 'Some data for dev'        parameter2: 'some data for dev'        parameter3: 'some data for dev'        parameter4: 'some data for dev'        parameter5: 'some data for dev'        parameter6: 'some data'


Another variant how to get parameters easy - you can just set ParameterBag to your service. You can do it in different ways - via arguments or via set methods. Let me show my example with set method.

So in services.yml you should add something like:

my_service:    class: MyService\Class    calls:        - [setParameterBag, ["@=service('kernel').getContainer().getParameterBag()"]]

and in class MyService\Class just add use:

use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

and create 2 methods:

/**                                                                                                                                                                       * Set ParameterBag for repository                                                                                                                                        *                                                                                                                                                                        * @param ParameterBagInterface $params                                                                                                                                   */public function setParameterBag(ParameterBagInterface $params){    $this->parameterBag = $params;}/**                                                                                                                                                                       * Get parameter from ParameterBag                                                                                                                                        *                                                                                                                                                                        * @param string $name                                                                                                                                                    * @return mixed                                                                                                                                                         */public function getParameter($name){    return $this->parameterBag->get($name);}

and now you can use in class:

$this->getParameter('your_parameter_name');


I believe you're supposed to pass the parameters individually. I think it's made that way by design so your service class is not dependent on the AppKernel. That way you can reuse your service class outside your Symfony project. Something that is useful when testing your service class.