【问题标题】:Zend Framework 2 Module Redirect The page isn't redirecting properlyZend Framework 2 模块重定向页面没有正确重定向
【发布时间】:2013-08-05 13:57:05
【问题描述】:

我在Zend Framework 2 的Module.php 中正确重定向有一些问题。 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 会对上述消息做出反应?

【问题讨论】:

    标签: redirect zend-framework2


    【解决方案1】:

    您将进入无限重定向循环。由于所有 Module 类都会在每次请求时实例化,因此每次都会调用此事件绑定。你需要添加类似的东西

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

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

    【讨论】:

    • 你说得对,我忘了添加路由条件。感谢您的快速答复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-10
    • 2013-02-08
    • 2012-10-29
    相关资源
    最近更新 更多