如何在没有重定向的情况下转发到Module.php的另一条路线?

时间:2014-02-23 21:37:10

标签: php authentication zend-framework2 zfcuser

我希望在没有网址重定向的情况下显示正在访问的网页的登录表单。是否可以在Module.php转发路由,即如果客户端访问/站/转发到“用户/登录”路由?没有外部重定向,我只想在私人页面上看到登录表单。

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;
}

此代码抛出Uncaught exception 'Zend\Mvc\Router\Exception\RuntimeException' with message 'Could not find prototype with name zfcuser/login'

所以问题如下:我怎样才能在Module.php上替换路线?

1 个答案:

答案 0 :(得分:0)

由于@samsonasik对另一个问题的评论,这个问题已经解决了。

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

所以我在没有外部重定向的情况下改变了路线。