Symfony2: How to inject ALL parameters in a service?

后端 未结 7 855
既然无缘
既然无缘 2020-12-28 14:14

How can I inject ALL parameters in a service?

I know I can do: arguments: [%some.key%] which will pass the parameters: some.key: \"value\"

7条回答
  •  轮回少年
    2020-12-28 14:47

    Note: I know that this solution is not BEST from design point of view, but it does the job, so please avoid down-voting.

    You can inject \AppKernel object and then access all parameters like this:

    config.yml:

    my_service:
        class: MyService\Class
        arguments: [@kernel]
    

    And inside MyService\Class:

    public function __construct($kernel)
    {
        $this->parameter = $kernel->getContainer()->getParameter('some.key');
        // or to get all:
        $this->parameters = $kernel->getContainer()->getParameterBag()->all();
    }
    

提交回复
热议问题