简体   繁体   中英

Kohana 2 Custom 404 error pages

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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM