How to inject the @request into a service?

后端 未结 9 2269
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:42

    The way I've found, and I'm sure it's probably not the best way (May not even be recommended), is to define the request service as synthetic.

    Edit: Indeed, this is not recommended, because it disables the scope sanity checks. This thread contains a good explanation of why Symfony is throwing that exception: http://groups.google.com/group/symfony-devs/browse_thread/thread/a7207406c82ef07a/e2626c00f5cb9749

    In your services.xml:

    
    
    
        
    
    

    Per the docs, it's better if you place your service in the request scope, or just inject the service container.

提交回复
热议问题