Question

I am trying to make custom error pages for 404, 403, 5xx errors. I followed the doc and created an error controller, override Kohana_Exception class and added a route.

Kohana_Exception Class

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

Error 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

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',
    ));

When I throw an HTTP_Exception_403 exception It does not redirect to welcome as it should according to the error controller. I think I am missing very simple thing here.

I debugged it and found execution point is not reaching to my controller. So it might be a problem in Route. What would be the proper route? what could be the real problem?

Was it helpful?

Solution

Your error routes to the Controller_Error_Handler, but your controller is called Controller_Error.

In case this will be a problem: Kohana expects a Controller_Error_Handler class in classes/error/handler.php

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top