How to inject the @request into a service?

后端 未结 9 2245
Happy的楠姐
Happy的楠姐 2020-12-01 05:53

When I try to inject the @request into any of my services, I get this exception:

ScopeWideningInjectionException: Scope Widening Injection detected:

9条回答
  •  攒了一身酷
    2020-12-01 06:24

    In Symfony 2.4, this has changed. Now, you can inject the 'request_stack' service.

    For example:

    use Symfony\Component\HttpFoundation\RequestStack;
    
    class MyService
    {
    
        protected $request;
    
        public function setRequest(RequestStack $request_stack)
        {
            $this->request = $request_stack->getCurrentRequest();
        }
    
    }
    

    In your config.yml:

    services:
        my.service:
            class: Acme\DemoBundle\MyService
            calls:
                - [setRequest, ["@request_stack"]]
    

    Full documentation is here: http://symfony.com/blog/new-in-symfony-2-4-the-request-stack

提交回复
热议问题