【问题标题】:Kohana 3.3 Redirect ExceptionKohana 3.3 重定向异常
【发布时间】:2013-10-11 18:37:38
【问题描述】:

希望您能帮我解决这个奇怪的问题:我正在尝试从 Controller 中重定向,但 Kohana 不断抛出我无法弄清楚原因的异常:

Cadastro.php 中的代码:

try{
     $this->redirect('/dados', 302);
} catch (Exception $e) {
                $this->response->body(Json_View::factory(array("line ".$e->getLine()." of file ".$e->getFile().":".$e->getMessage()." - trace as string: ".$e->getTraceAsString())));
}            }

上面代码中异常返回的堆栈跟踪信息是:

#0 C:\\xampp\\htdocs\\grademagica\\system\\classes\\Kohana\\HTTP.php(33): Kohana_HTTP_Exception::factory(302)
#1 C:\\xampp\\htdocs\\grademagica\\system\\classes\\Kohana\\Controller.php(127): Kohana_HTTP::redirect('\/dados', 302)
#2 C:\\xampp\\htdocs\\grademagica\\modules\\grademagica\\classes\\Controller\\Cadastro.php(123): Kohana_Controller::redirect('\/dados', 302)
#3 C:\\xampp\\htdocs\\grademagica\\system\\classes\\Kohana\\Controller.php(84): Controller_Cadastro->action_signin()
#4 [internal function]: Kohana_Controller->execute()
#5 C:\\xampp\\htdocs\\grademagica\\system\\classes\\Kohana\\Request\\Client\\Internal.php(97): ReflectionMethod->invoke(Object(Controller_Cadastro))
#6 C:\\xampp\\htdocs\\grademagica\\system\\classes\\Kohana\\Request\\Client.php(114): Kohana_Request_Client_Internal->execute_request(Object(Request), Object(Response))
#7 C:\\xampp\\htdocs\\grademagica\\system\\classes\\Kohana\\Request.php(990): Kohana_Request_Client->execute(Object(Request))
#8 C:\\xampp\\htdocs\\grademagica\\index.php(123): Kohana_Request->execute()
#9 {main}

Cadastro.php 中的第 123 行是“$this->redirect('/dados', 302);”,如上所述。 有谁可以帮助我展示我做错了什么?我正在遵循documentation的确切方向

谢谢

【问题讨论】:

    标签: php exception redirect kohana kohana-3.3


    【解决方案1】:

    让我们看看会发生什么。

    你打电话给$this->redirect('/dados', 302);,我们看一下它的源码:

    public static function redirect($uri = '', $code = 302)
    {
        return HTTP::redirect($uri, $code);
    }
    

    好的,我们知道$this->redirect('/dados') 就足够了,接下来让我们看看 HTTP::redirect():

    public static function redirect($uri = '', $code = 302)
    {
        $e = HTTP_Exception::factory($code);
    
        if ( ! $e instanceof HTTP_Exception_Redirect)
            throw new Kohana_Exception('Invalid redirect code \':code\'', array(
                ':code' => $code
            ));
    
        throw $e->location($uri);
    }
    

    它会创建一个异常(HTTP_Exception_$code)然后抛出它。

    异常应该冒泡到Request_Client_Internal::execute_request(),下面的catch块应该处理它:

    catch (HTTP_Exception $e)
    {
        // Get the response via the Exception
        $response = $e->get_response();
    }
    

    但是由于您捕获了异常,因此它不会冒泡。这是修复它的一种方法。

    try{
         $this->redirect('/dados', 302);
    } catch (HTTP_Exception_Redirect $e) {
        throw $e;
    } catch (Exception $e) {
                    $this->response->body(Json_View::factory(array("line ".$e->getLine()." of file ".$e->getFile().":".$e->getMessage()." - trace as string: ".$e->getTraceAsString())));
    }
    

    【讨论】:

    • 感谢您的帮助 Darsstar,但它对我来说并不完全有效。正如您所指出的,我可以看到引发异常的位置,但我仍然不明白为什么会发生这种情况。然而,困扰我的是它仍然没有重定向。我已经通过 Jquery 的 ajax 调用了控制器,以便在重定向之前验证服务器端的表单。现在,在您的帮助下,ajax 调用不再返回异常数据,它返回我要重定向到的页面的源代码。你知道为什么吗?
    • 至于为什么抛出异常,这就是 Kohana 开发人员决定他们将通过提供的辅助方法进行重定向的方式。至于为什么你得到源代码,我不知道......除非你的意思是Kohana error page。你可以试试 $this->response->status(302)->headers('Location', '/dados');看看这是否更好。 (您可能还想包含来自HTTP_Exception_Redirect::location() 的逻辑。)
    • 这是因为 $this->request('/dados') 应该通过抛出 HTTP_Exception_Redirect 实例来实现。但它似乎永远不会到达 Request_Client_Internal::execute_request() 因为它被夹在两者之间。这可能表明您的控制器正在变胖。 $this->response = Response::factory()->status(302)->headers('Location', '/dados'); 也稍微干净一些,因为就 Response 对象而言,您从一个干净的石板开始。
    • 我认为通过 ajax 调用控制器时无法从控制器内部重定向。显然,它将目标(重定向)页面识别为 ajax 函数的返回。所以我所做的是返回一个 json 以指示需要重定向,并且 URL 将由客户端 javascript 测试处理以从客户端重定向。但是,Darsstar 描述的所有方法都适用于同步调用控制器(而不是通过 ajax)。
    猜你喜欢
    • 2014-10-15
    • 1970-01-01
    • 1970-01-01
    • 2013-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多