php - Kohana 3.2自定义错误处理

标签 php error-handling kohana http-status-codes kohana-3.2

我正在尝试为404、403、5xx错误创建自定义错误页面。我遵循了doc并创建了一个错误 Controller ,覆盖了Kohana_Exception类并添加了一条路由。

Kohana_Exception类

class Kohana_Exception extends Kohana_Kohana_Exception {
    public static function handler(Exception $e) {
        if (Kohana::DEVELOPMENT === Kohana::$environment) {
            parent::handler($e);
        } else {
            try {
                Kohana::$log->add(Log::ERROR, parent::text($e));
                $attributes = array('action' => 500, 'message' => rawurlencode($e->getMessage()));
                if ($e instanceof HTTP_Exception) {
                    $attributes['action'] = $e->getCode();
                }
                // Error sub-request.
                echo Request::factory(Route::get('error')->uri($attributes))->execute()->send_headers()->body();
            }
            catch(Exception $e) {
                // Clean the output buffer if one exists
                ob_get_level() and ob_clean();
                // Display the exception text
                echo parent::text($e);
                // Exit with an error status
                exit(1);
            }
        }
    }
}

错误 Controller
<?php defined('SYSPATH') OR die('No direct access allowed.');
class Controller_Error extends Webim_Template {
    public function before() {
        parent::before();
        $this->template->page = URL::site(rawurldecode(Request::$initial->uri()));
        // Internal request only!
        if (Request::$initial !== Request::$current) {
            if ($message = rawurldecode($this->request->param('message'))) {
                $this->template->message = $message;
            }
        } else {
            $this->request->action(404);
        }
        $this->response->status((int)$this->request->action());
    }
    public function action_404() {
        $this->template->title = __('404 Not Found');
        // Here we check to see if a 404 came from our website. This allows the
        // webmaster to find broken links and update them in a shorter amount of time.
        if (isset($_SERVER['HTTP_REFERER']) AND strstr($_SERVER['HTTP_REFERER'], $_SERVER['SERVER_NAME']) !== FALSE) {
            // Set a local flag so we can display different messages in our template.
            $this->template->local = TRUE;
        }
        // HTTP Status code.
        $this->response->status(404);
    }
    public function action_403() {
        $this->request->redirect('welcome');
    }
    public function action_503() {
        $this->template->title = __('Maintenance Mode');
    }
    public function action_500() {
        $this->template->title = __('Internal Server Error');
    }
}

路线
Route::set('error', 'error/<action>(/<message>)', 
        array('action' => '[0-9]++', 'message' => '.+'))
            ->defaults(array(
            'controller' => 'error_handler'
));

Route::set('default', '(<controller>(/<action>(/<id>)))')
    ->defaults(array(
        'controller' => 'welcome',
        'action'     => 'index',
    ));

当我引发HTTP_Exception_403异常时,它不会按照错误 Controller 的要求重定向到welcome。我想我在这里错过了非常简单的事情。

我调试了它,发现执行点没有到达我的 Controller 。因此,这可能是Route中的问题。正确的路线是什么?真正的问题是什么?

最佳答案

您的错误将路由到Controller_Error_Handler,但您的 Controller 称为Controller_Error。

万一这是个问题:Kohana希望在classes/error/handler.php中有一个Controller_Error_Handler类。

关于php - Kohana 3.2自定义错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11747843/

相关文章:

c# - .NET Web API中未更新服务层ModelState

c# - HTMLAgilityPack Asp.net C#错误处理

php - 如何更改 Kohana 3 中使用的默认数据库?

mysql - Kohana ORM,重写 ORDER BY 和 GROUP BY 查询

php - 从 wordpress 函数获取结果 SQL

php - 将多个值插入同一列名称+覆盖/更新现有条目?

c# - C#-mscorlib.dll中未处理的异常

php - Kohana3 : Different . htaccess rewritebase 和 kohana base_url 用于开发和生产环境

php - 计算数据库中的行数并回显所需的行号

php - 在正常的 PHP session 中使用 CodeIgniter,在何处添加 session_start();