gpt4 book ai didi

php - Kohana 3.3 重定向异常

转载 作者:行者123 更新时间:2023-12-02 21:47:12 25 4
gpt4 key购买 nike

希望你能帮助我解决这个奇怪的问题:我正在尝试从 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 的确切指示

谢谢

最佳答案

让我们看看会发生什么。

你调用了$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 block 应该处理它:

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

关于php - Kohana 3.3 重定向异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19325056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com