How to forward to another route at Module.php without redirect?

主宰稳场 提交于 2019-12-23 05:19:21

问题


I wish to show login form at page being accessed without url redirection. Is it possible to forward route at Module.php, i.e. if client accesses /stations/ forward it to "user/login" route? Without external redirect, I just want to see login form at private pages.

public function onBootstrap(MvcEvent $e){
    # .... other
    $eventManager->attach(MvcEvent::EVENT_ROUTE, array($this, 'checkAuth'), -100);
}
public function checkAuth(MvcEvent $e){
    # ... get auth service 
    if (!$auth->hasIdentity()) {
        # smth like this
        $router   = $e->getRouter()->setRoutes(array('zfcuser/login'));
        return $router;
    }
    return;
}

This code throws Uncaught exception 'Zend\Mvc\Router\Exception\RuntimeException' with message 'Could not find prototype with name zfcuser/login'.

So the question is the following: how can I substitute route at Module.php?


回答1:


The problem has been solved thanks to @samsonasik comment to another question.

public function checkAuth(MvcEvent $e){
    # ... get auth service 
    if (!$auth->hasIdentity()) {
        $e->getRouteMatch()
                     ->setParam('controller', 'zfcuser')
                     ->setParam('action', 'login');
    }
    return;
}

So I have changed route without external redirection.



来源:https://stackoverflow.com/questions/21974916/how-to-forward-to-another-route-at-module-php-without-redirect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!