php - ZF2 - Trigger MvcEvent::EVENT_DISPATCH_ERROR from View Controller -
i using restful controller , on conditions, trigger mvcevent::event_dispatch_error
, stop execution of controller after. in module
class, have attached event listener can't find way trigger view controller.
my module
code is:
public function onbootstrap(mvcevent $mvcevent) { $eventmanager = $mvcevent->getapplication() ->geteventmanager(); $eventmanager->attach(array(mvcevent::event_dispatch_error, mvcevent::event_render_error), array($this, 'error')); } public function error(mvcevent $mvcevent) { echo $mvcevent->geterror(); die(); }
and controller code is:
public function indexaction() { $mvcevent = $this->getevent(); $mvcevent->seterror('test-error-code'); $mvcevent->gettarget()->geteventmanager()->trigger(mvcevent::event_dispatch_error, $mvcevent); return; }
i think problem you're not attaching application's sharedeventmanager
. can use controller's own event manager trigger event.
try this:
module.php
public function onbootstrap(mvcevent $mvcevent) { $eventmanager = $mvcevent->getapplication()->geteventmanager()->getsharedmanager(); $eventmanager->attach('zend\stdlib\dispatchableinterface', mvcevent::event_dispatch_error, array($this, 'error')); }
controller
public function indexaction() { $mvcevent = $this->getevent(); $mvcevent->seterror('test-error-code'); $this->geteventmanager()->trigger(mvcevent::event_dispatch_error, $mvcevent); return; }
Comments
Post a Comment