How to inject the @request into a service?

后端 未结 9 2257
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:43

    If you can't use RequestStack directly, you could create a factory service that returns the current request using RequestStack.

    # services.yml
    app.request:
        class: Symfony\Component\HttpFoundation\RequestStack
        factory: [ @request_stack, getCurrentRequest ]
    

    Then you can access the current request using the app.request service.

提交回复
热议问题