Redirect Zend Framework 2模块重定向页面isn';不能正确地重定向

Redirect Zend Framework 2模块重定向页面isn';不能正确地重定向,redirect,zend-framework2,Redirect,Zend Framework2,我对Module.php中Zend Framework 2中的正确重定向有一些问题。 Iceweasel浏览器显示以下信息: “页面未正确重定向” 我的代码很简单,但似乎产生了一些问题: $eventManager->attach(\Zend\Mvc\MvcEvent::EVENT_DISPATCH, function ($e) { $auth = new AuthenticationService();

我对Module.php中Zend Framework 2中的正确重定向有一些问题。 Iceweasel浏览器显示以下信息: “页面未正确重定向”

我的代码很简单,但似乎产生了一些问题:

$eventManager->attach(\Zend\Mvc\MvcEvent::EVENT_DISPATCH, function ($e) {
    $auth = new AuthenticationService();                                        
    if (!$auth->hasIdentity()) {
        $url = $e->getRouter()->assemble(array(), array('name' => 'login'));
        $response = $e->getResponse();
        $response->getHeaders()->addHeaderLine('Location', $url);
        $response->setStatusCode(302);
        $response->sendHeaders();
        return $response;
    }
});

有谁能告诉我一些zf2提示,为什么Iceweasel会对上述消息做出反应?

您将进入一个无限重定向循环。由于所有模块类都在每个请求上实例化,因此每次都会调用此事件绑定。您需要添加以下内容

$route = $e->getRouteMatch()->getMatchedRouteName();
if(!$auth->hasIdentity() && $route !== 'login') {
...
}

这样,当您到达登录页面时,它不会再次尝试重定向。

您是对的,我忘了添加路由条件。谢谢你的快速回答。