ZF2 How to access event manager in controller's constructor -
how access event manager in controller constructor ? when call event manager in constructor , error appears :
zend\servicemanager\servicemanager::get unable fetch or create instance event
you don't have access service manager @ point, it's injected once object has been instantiated.
you move code triggered ondispatch() rather in contructor:
/** * execute request * * @param mvcevent $e * @return mixed * @throws exception\domainexception */ public function ondispatch(mvcevent $e) { // here // or use events system attach ondispatch event // rather putting code directly controller, // better option return parent::ondispatch($e); }
i use events attach ever need, rather using controller
module.php
/** * initialize * * @param \mis\modulemanager */ public function init(modulemanager $manager) { $events = $manager->geteventmanager(); $sharedevents = $events->getsharedmanager(); $sharedevents->attach(__namespace__, 'dispatch', function($e) { /* @var $e \zend\mvc\mvcevent */ // fired when actioncontroller under namespace dispatched. $controller = $e->gettarget(); $routematch = $e->getroutematch(); /* @var $routematch \zend\mvc\router\routematch */ $routename = $routematch->getmatchedroutename(); // attach method here need }, 100); }
Comments
Post a Comment