我正在为Kohana编写错误处理程序。控制器Controller_Errors
(继承Controller_Kohana_Errors
)包含action_404
,action_403
,action_500
等操作。
路线:
Route::set('errors','errors/<action>', array (
'action' => '[\d]{3}',
))
->defaults(array(
'controller' => 'errors',
));
包含示例bootstrap.php
try {
$response = Request::factory()->execute();
} catch (Exception $e) {
// If we are in development and the error wasn't a 404, show the stack trace.
// Otherwise, show a nice error.
if ((Kohana::$environment == KOHANA::DEVELOPMENT) AND ( ! ($e->getCode() == 404 OR $e->getCode() == 403))) {
throw $e;
}
// Log the error
Kohana::$log->add(Log::ERROR, Kohana_Exception::text($e));
$error_code = 500;
if ($e instanceof ReflectionException) {
$error_code = 404;
}
if ($e instanceof HTTP_Exception)
{
$error_code = $e->getCode();
}
$request = Request::factory('errors/'.$error_code);
if (*__NOT SURE WHAT GOES HERE__*) {
$request = Request::factory('errors/500');
}
$response = $request->execute();
}
// Send the headers and echo the response
echo $response->send_headers()->body();
我想允许人们扩展我的课程,所以我需要一些方法来检查是否存在某个操作,如果不存在,则更改为显示500错误。 (请参阅说明*__NOT SURE WHAT GOES HERE__*
的行。)
答案 0 :(得分:1)
我认为以下代码会这样做:
if ($request->status == 404 && $error_code != 404) ...
在这种情况下,您知道404不是来自原始请求,因此必须来自为加载错误代码的错误页面。