How to setup a custom 404 page for a Kohana v3 app

前端 未结 3 671
孤独总比滥情好
孤独总比滥情好 2021-01-03 05:02

How can this be done? I\'m trying to do this for about half an hour and it\'s getting pretty annoying. You would this this should be an basic and easy thing to setup for a f

相关标签:
3条回答
  • 2021-01-03 05:27

    Since v3.1 the Kohana guide incldues a tutorial on custom error pages which show a much cleaner way of solving this question.

    0 讨论(0)
  • 2021-01-03 05:32

    All you need to do is set the path to a different view in your bootstrap.php add:

    Kohana_Exception::$error_view = 'error/myErrorPage';
    

    that will parse all the variables currently being parsed to the error page that lives in:

    system/views/kohana/error.php
    

    ie:

    <h1>Oops [ <?= $code ?> ]</h1>
    <span class="message"><?= html::chars($message) ?></span>
    
    0 讨论(0)
  • 2021-01-03 05:35

    Replace the last line of bootstrap.php with:

    /**
    * Set the production status
    */
    define('IN_PRODUCTION', FALSE);
    
    /**
    * Execute the main request. A source of the URI can be passed, eg: $_SERVER['PATH_INFO'].
    * If no source is specified, the URI will be automatically detected.
    */
    $request = Request::instance();
    
    try
    {
        $request->execute();
    }
    catch (Kohana_Exception404 $e)
    {
        $request = Request::factory('error/404')->execute();
    }
    catch (Kohana_Exception403 $e)
    {
        $request = Request::factory('error/403')->execute();
    }
    catch (ReflectionException $e)
    {
        $request = Request::factory('error/404')->execute();
    }
    catch (Kohana_Request_Exception $e)
    {
        $request = Request::factory('error/404')->execute();
    }
    catch (Exception $e)
    {
        if ( ! IN_PRODUCTION )
        {
            throw $e;
        }
    
        $request = Request::factory('error/500')->execute();
    }
    
    echo $request->send_headers()->response;
    

    Create new controller "error.php":

    <?php defined('SYSPATH') or die('No direct script access.');
    
    class Controller_Error extends Controller {
    
        public function action_404()
        {
            $this->request->status = 404;
            $this->request->headers['HTTP/1.1'] = '404';
            $this->request->response = 'error 404';
        }
    
        public function action_403()
        {
            $this->request->status = 403;
            $this->request->headers['HTTP/1.1'] = '403';
            $this->request->response = 'error 403';
        }
    
        public function action_500()
        {
            $this->request->status = 500;
            $this->request->headers['HTTP/1.1'] = '500';
            $this->request->response = 'error 500';
        }
    } // End Error
    

    Create two files (exception404.php и exception403.php) in "kohana" folder:

    <?php defined('SYSPATH') or die('No direct access');
    
    class Kohana_Exception403 extends Kohana_Exception {
    
        public function __construct($message = 'error 403', array $variables = NULL, $code = 0)
        {
            parent::__construct($message, $variables, $code);
        }
    
    } // End Kohana_Exception 403
    
    
    <?php defined('SYSPATH') or die('No direct access');
    
    class Kohana_Exception404 extends Kohana_Exception {
    
        public function __construct($message = 'error 404', array $variables = NULL, $code = 0)
        {
            parent::__construct($message, $variables, $code);
        }
    
    } // End Kohana_Exception 404
    

    Now you can manually throw 404 and 403 errors (you can't throw error 500 ;)

    throw new Kohana_Exception404;
    throw new Kohana_Exception403;
    
    0 讨论(0)
提交回复
热议问题