Kohana 3.3重定向异常

And*_*ade 2 php redirect exception kohana kohana-3.3

希望你能帮助我解决这个奇怪的问题:我正试图从一个控制器内重定向,但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())));
}            }
Run Code Online (Sandbox Code Playgroud)

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

#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}
Run Code Online (Sandbox Code Playgroud)

Cadastro.php中的第123行是"$ this-> redirect('/ dados',302);",如上所述.有谁能帮我展示我做错了什么?我按照文档的确切说明进行操作

谢谢

Dar*_*tar 5

让我们看看发生了什么.

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

public static function redirect($uri = '', $code = 302)
{
    return HTTP::redirect($uri, $code);
}
Run Code Online (Sandbox Code Playgroud)

好的,我们学到了$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);
}
Run Code Online (Sandbox Code Playgroud)

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

异常应冒泡到Request_Client_Internal :: execute_request(),其中以下catch块应该处理它:

catch (HTTP_Exception $e)
{
    // Get the response via the Exception
    $response = $e->get_response();
}
Run Code Online (Sandbox Code Playgroud)

但是,既然你抓住了这个例外,它就不会起泡.这是解决它的一种方法.

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())));
}
Run Code Online (Sandbox Code Playgroud)