Zend 2 redirect() module error -
i have made checklogin fucntion check $auth->getidentity(),
getting following error:
fatal error: call undefined method admin\module::redirect() in c:\xampp\websites\zend2\module\admin\module.php on line 51
how can fix this?
public function onbootstrap(mvcevent $e) { $eventmanager = $e->getapplication()->geteventmanager(); $moduleroutelistener = new moduleroutelistener(); $moduleroutelistener->attach($eventmanager); $app = $e->getparam('application'); $app->geteventmanager()->attach('render', array($this, 'setlayouttitle')); $moduleroutelistener->attach($eventmanager); // add event $eventmanager->attach('render', array($this, 'checklogin')); } public function checklogin() { $auth = new authenticationservice(); if( $auth->getidentity() == null ){ return $this->redirect()->toroute('/admin/login'); }else{ return $this->redirect()->toroute('/admin'); } }
that's because there no redirect
method in module
class.
i suggest move check in dispatch
event, because dispatch
event triggered before controller action dispatched.
so need change listener method setting in onbootstrap module method:
public function onbootstrap(mvcevent $e) { // ... $eventmanager->attach(mvcevent::event_dispatch, array($this, 'checklogin')); // ... }
then in listener method got mvcevent
target
matched controller class:
public function checklogin(mvcevent $e) { $controller = $e->gettarget(); if ($somecondition) { return $controller->plugin('redirect')->toroute('your/route/name'); } }
Comments
Post a Comment