本文介绍了如何将@request注入服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试将@request注入我的任何服务时,我收到这个例外:

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

最好的方法是什么?我应该尝试设置这个 strict = false ,以及如何或不应该注入请求服务,而是每次调用函数I通过我的控制器将其传递给服务需要?

What is the best way to proceed? Should I try to set this strict=false and how, or should I NOT inject the request service, but rather pass it to the service through my controller each time I call functions I need?

其他可能性是注入内核并从中取出,但在我的服务中,我只使用@router和@request,因此注入整个内核将不合理。

Other possibility would be to inject the kernel and take it from there, but in my service I am using only @router and @request, so injecting the whole kernel would be irrational.

谢谢!

推荐答案

对官方文件说明了一些误解。在大多数情况下,您确实要直接在服务元素上使用 scope =request属性注入请求。这使得范围扩大消失。

I think there may have been some misunderstanding about what the official documentation says. In most cases you do want to inject the request directly with a scope="request" attribute on the service element. This makes the Scope Widening go away.

<service 
    id="zayso_core.openid.rpx" 
    class="Zayso\CoreBundle\Component\OpenidRpx" public="true" scope="request">

yml

zayso_core.openid.rpx: 
    class: Zayso\CoreBundle\Component\OpenidRpx
    public: true
    scope: request

只有特定的特殊情况,例如需要注入容器的Twig扩展名。

It's only in specific special cases such as Twig extensions where you need to inject the container.

在范围页面中甚至没有提到内核。注入内核比注入容器要差得多(概念上)。

And kernel is not even mentioned in the page on scopes. Injecting the kernel is far worse (conceptually) than injecting a container.

更新:对于S2.4及更高版本,请使用@ Blowski的答案。

UPDATE: For S2.4 and newer, use @Blowski's answer below.

这篇关于如何将@request注入服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 23:00