如何在监听器中注入@request_stack或@request
How to inject @request_stack or @request in listener
我有这个听众:
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpFoundation\Request;
class ControllerListener
{
public function onKernelController( FilterControllerEvent $event, Request $request )
{
if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) {
$controllers = $event->getController();
if (is_array( $controllers )) {
$controller = $controllers[0];
if (is_object( $controller )) {
if (method_exists( $controller, 'parControl' )) {
$controller->parControl(
$controller->getRequest(),
$controller->getRequest()->get( 'action' ),
$controller->getRequest()->get( 'context' ),
$controller->getRequest()->get( 'page' ),
$controller->getRequest()->get( 'case' ),
$controller->getRequest()->get( 'subset' )
);
}
if (method_exists( $controller, 'adminProjectControl' )) {
// to this method I need to pass Request $request
$controller->adminProjectControl($request);
}
if (method_exists( $controller, 'accessControl' )) {
$controller->accessControl();
}
}
}
}
}
public function onKernelResponse( FilterResponseEvent $event )
{
Utilities::flashDebugLoginMessage();
}
}
它在 services.yml
:
定义为服务
pdone.listener.preexecute:
class: GroupDCA\PDOneBundle\Listener\ControllerListener
arguments: [@request]
public: true
scope: request
tags:
- { name: kernel.event_listener, event: kernel.controller, method: onKernelController }
- { name: kernel.event_listener, event: kernel.response, method: onKernelResponse }
但我应该做错了,因为我得到了这三个错误:
ContextErrorException in ControllerListener.php line 12: Catchable
Fatal Error: Argument 2 passed to
GroupDCA\PDOneBundle\Listener\ControllerListener::onKernelController()
must be an instance of Symfony\Component\HttpFoundation\Request,
string given
ContextErrorException in ControllerListener.php line 12: Catchable
Fatal Error: Argument 2 passed to
GroupDCA\PDOneBundle\Listener\ControllerListener::onKernelController()
must be an instance of Symfony\Component\HttpFoundation\Request,
string given
LogicException in bootstrap.php.cache line 3118: No listeners of the
"kernel.exception" event set a Response
那么,如何将 Request 注入监听器呢?我可以这样做吗?我在这里也阅读了 this post 但不知道如何做到这一点。
您正在将请求对象从您的服务定义传递给 ControllerListener 构造函数,而不是传递给 onKernelController 方法。
改为:
protected $request;
public function __construct (Request $request) {
$this->request = $request;
}
这应该可以解决您的问题并使请求对象可用。
FilterControllerEvent
已经 gives you access to the request:
public function onKernelController(FilterControllerEvent $event)
{
$request = $event->getRequest();
// ...
}
如果您需要在事件未提供请求的侦听器中请求,请始终使用 the request stack:
use Symfony\Component\HttpFoundation\RequestStack;
class MyService
{
private $requestStack;
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}
public function doSomething()
{
$request = $this->requestStack->getCurrentRequest();
// ...
}
}
注入请求已弃用,将被删除。 Request stack is the way to go。也不要使用 request
范围。
我有这个听众:
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpFoundation\Request;
class ControllerListener
{
public function onKernelController( FilterControllerEvent $event, Request $request )
{
if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) {
$controllers = $event->getController();
if (is_array( $controllers )) {
$controller = $controllers[0];
if (is_object( $controller )) {
if (method_exists( $controller, 'parControl' )) {
$controller->parControl(
$controller->getRequest(),
$controller->getRequest()->get( 'action' ),
$controller->getRequest()->get( 'context' ),
$controller->getRequest()->get( 'page' ),
$controller->getRequest()->get( 'case' ),
$controller->getRequest()->get( 'subset' )
);
}
if (method_exists( $controller, 'adminProjectControl' )) {
// to this method I need to pass Request $request
$controller->adminProjectControl($request);
}
if (method_exists( $controller, 'accessControl' )) {
$controller->accessControl();
}
}
}
}
}
public function onKernelResponse( FilterResponseEvent $event )
{
Utilities::flashDebugLoginMessage();
}
}
它在 services.yml
:
pdone.listener.preexecute:
class: GroupDCA\PDOneBundle\Listener\ControllerListener
arguments: [@request]
public: true
scope: request
tags:
- { name: kernel.event_listener, event: kernel.controller, method: onKernelController }
- { name: kernel.event_listener, event: kernel.response, method: onKernelResponse }
但我应该做错了,因为我得到了这三个错误:
ContextErrorException in ControllerListener.php line 12: Catchable Fatal Error: Argument 2 passed to GroupDCA\PDOneBundle\Listener\ControllerListener::onKernelController() must be an instance of Symfony\Component\HttpFoundation\Request, string given
ContextErrorException in ControllerListener.php line 12: Catchable Fatal Error: Argument 2 passed to GroupDCA\PDOneBundle\Listener\ControllerListener::onKernelController() must be an instance of Symfony\Component\HttpFoundation\Request, string given
LogicException in bootstrap.php.cache line 3118: No listeners of the "kernel.exception" event set a Response
那么,如何将 Request 注入监听器呢?我可以这样做吗?我在这里也阅读了 this post 但不知道如何做到这一点。
您正在将请求对象从您的服务定义传递给 ControllerListener 构造函数,而不是传递给 onKernelController 方法。
改为:
protected $request;
public function __construct (Request $request) {
$this->request = $request;
}
这应该可以解决您的问题并使请求对象可用。
FilterControllerEvent
已经 gives you access to the request:
public function onKernelController(FilterControllerEvent $event)
{
$request = $event->getRequest();
// ...
}
如果您需要在事件未提供请求的侦听器中请求,请始终使用 the request stack:
use Symfony\Component\HttpFoundation\RequestStack;
class MyService
{
private $requestStack;
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}
public function doSomething()
{
$request = $this->requestStack->getCurrentRequest();
// ...
}
}
注入请求已弃用,将被删除。 Request stack is the way to go。也不要使用 request
范围。