php - How to inject @request_stack or @request in listener -
i have 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' )) { // method need pass request $request $controller->adminprojectcontrol($request); } if (method_exists( $controller, 'accesscontrol' )) { $controller->accesscontrol(); } } } } } public function onkernelresponse( filterresponseevent $event ) { utilities::flashdebugloginmessage(); } }
and it's defined service @ 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 }
but should doing wrong since got 3 errors:
contexterrorexception in controllerlistener.php line 12: catchable fatal error: argument 2 passed groupdca\pdonebundle\listener\controllerlistener::onkernelcontroller() must instance of symfony\component\httpfoundation\request, string given
contexterrorexception in controllerlistener.php line 12: catchable fatal error: argument 2 passed groupdca\pdonebundle\listener\controllerlistener::onkernelcontroller() must instance of symfony\component\httpfoundation\request, string given
logicexception in bootstrap.php.cache line 3118: no listeners of "kernel.exception" event set response
so, how inject request listener? can that? have read this post here not how this.
filtercontrollerevent
gives access request:
public function onkernelcontroller(filtercontrollerevent $event) { $request = $event->getrequest(); // ... }
if need request in listeners event not provide request, use 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(); // ... } }
injecting request deprecated , removed. request stack way go. don't use request
scope either.
Comments
Post a Comment