检查Kohana中是否存在Action

时间:2012-05-08 17:13:56

标签: kohana

我正在为Kohana编写错误处理程序。控制器Controller_Errors(继承Controller_Kohana_Errors)包含action_404action_403action_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__*的行。)

1 个答案:

答案 0 :(得分:1)

我认为以下代码会这样做:

if ($request->status == 404 && $error_code != 404) ...

在这种情况下,您知道404不是来自原始请求,因此必须来自为加载错误代码的错误页面。