Zend Framework 2: How to place a redirect into a module, before the application reaches a controller

后端 未结 4 1393
礼貌的吻别
礼貌的吻别 2020-12-28 21:19

Let\'s say we have a module named Cart and want to redirect users if some condition is met. I want to place a redirect at the module bootstrapping stage, before the applicat

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-28 21:49

    Of course it gives you an error because you must attach your listener to an event. In the folllowing example i use SharedManager and i attach the listener to AbstractActionController.

    Of course you can attach your listener to another event. Below is just a working example to show you how it works. For mor info visit http://framework.zend.com/manual/2.1/en/modules/zend.event-manager.event-manager.html.

    public function onBootstrap($e)
    {
        $e->getApplication()->getEventManager()->getSharedManager()->attach('Zend\Mvc\Controller\AbstractActionController', 'dispatch', function($e) {
            $controller = $e->getTarget();
            if (something.....) {
                $controller->plugin('redirect')->toRoute('yourroute');
            }
        }, 100);
    }
    

提交回复
热议问题