1

In Kohana 2 you can override the default error page by creating a custom kohana_error_page.php file and putting it in your view directory. However, this overrides ALL errors not just 404's. In the case of 500 errors, I'd still like to get the friendly orange kohana error page.

Does anyone have experience in doing this?

tereško
  • 58,060
  • 25
  • 98
  • 150
Ray
  • 40,256
  • 21
  • 101
  • 138

2 Answers2

3

You can do this in KO2 quite easily with hooks. If you look at events you'll find system.404 which you'll need to replace using something like:

<?php defined('SYSPATH') or die('No direct script access.');

// Replace the default kohana 404
Event::replace('system.404', array('Kohana', 'show_404'), 
    array('hook_404', 'show'));

class hook_404 {
    public function show()
    {
        // first param is URI of page, second param is template to use
        Kohana::show_404(FALSE, 'custom_404');
    }

}

Save this in the hooks directory in your app folder (or in a module). Don't forget to enable hooks in your config:

$config['enable_hooks'] = TRUE;

And add your custom 404 view: views/custom_404.php.

Note: these won't display if you have $config['display_errors'] set to FALSE in your config.php (which it probably should be if you're IN_PRODUCTION right?). For that you need to output something and die eg. replace Kohana::show_404 with the following:

require Kohana::find_file('views', 'custom_404');
die();
George
  • 2,860
  • 18
  • 31
2

I have not tested this!! So backup your code before you do this.

If I'm not mistaken, Kohana2 has hard-coded exception handling and there is no pretty way to add new exceptions. To work around that, you will have to make change in the core.

In the file system/i18n/en_US/errors.php

  • add new entry:

    E_PAGE_ACCESS_DENIED => array( 1, 'Access Denied', ' -- message text-- ')
    

In the file system/i18n/en_US/core.php

  • add new entry:

    'page_access_denied' => 'You are not permitted to access %s .'
    

In the system/core/Koahana.php:

  • near the top of Kohana::setup() method, add new constant:

    define('E_PAGE_ACCESS_DENIED', 69);
    
  • register the the event for your custom error ( somewhere near the end of same Kohana::setup() you will see registration for 404th error) :

    Event::add('system.403', array('Kohana', 'show_403'));
    
  • next, find the location for Kohana::show_404() and create you own method:

    public static function show_403($page = FALSE, $template = FALSE)
    {
        throw new Kohana_403_Exception($page, $template);
    }
    
  • scroll down to the bottom of the file .. there you will find the class definition for the Error_404_Exception ... make one for 403. Don't forget to:

    1. define new variable protected $template = 'file_name_for_template';
    2. protected $code = E_PAGE_ACCESS_DENIED;
    3. Exception::__construct(Kohana::lang('core.page_access_denied', $page));
    4. header('HTTP/1.1 403 Forbidden');

    the template file will have to be located at system/views/

Now you should be able to call Event::run('system.403'); from anywhere in the application.

tereško
  • 58,060
  • 25
  • 98
  • 150
  • This is interesting for adding/calling new error levels, but I'm concerned with the existing ones. Say a 'deprecated' or 'variable not defined' php warning is thrown. Kohana treats them as a 500 error and shows the orange error page, just like a 404 error. I can override the default error page with a kohana_error_page.php file, but how do I serve different error page views differently? Like if I wanted green page for 404's and a red page for 500's. – Ray Mar 22 '12 at 13:16
  • @Ray , I understood that you wanted to add a custom message specifically for for some error code. In that case you will to locate `public static function exception_handler()`. There will be an if with `$PHP_ERROR` .. and in it .. `$template` variable. hanging that should let you land in different error page. – tereško Mar 22 '12 at 13:28
  • Thanks, I was hoping there was some way to not have to modify the system files, but I guess that's the only way. I put in a temporary hack just checking the string $error in the kohana_error_page.php for the text of the error like'Page Not Found' and then switch between alternate pages, but that's a little cheesy. – Ray Mar 22 '12 at 13:38
  • @Ray , well .. the 2.x releases were quite bad. There had a lot of stuff hard-coded. You could of course always keep using the same template with the variable. – tereško Mar 22 '12 at 13:54
  • 1
    That's my biggest gripe in general with kohana--they ditch old versions and have no migration paths up to new releases. I'm at a company with a number of sites and now we're stuck at 2.x. I don't use their 'model' functionality or many helpers, so one day it shouldn't be too bad to move out... – Ray Mar 22 '12 at 14:12