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

依然范特西╮ 提交于 2019-12-18 16:59:03

问题


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 framework like this. I hope maybe there's an easy way i missed, because i'm starting to thing i should not chose this framework at all if such basic tings are so hard to setup.

This is in my bootstrap.php file that should do the trick.

if ( ! defined('SUPPRESS_REQUEST'))
{
    /**
     * 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
    {
        // Attempt to execute the response
        $request->execute();
    }
    catch (Exception $e)
    {
        if (Kohana::$environment === Kohana::DEVELOPMENT)
        {
            // Just re-throw the exception
            throw $e;
        }
        echo "ok";
        // Create a 404 response
        $request->status = 404;
        $view = View::factory('error404');
        $request->response = $view->render();
    }

    echo $request->send_headers()->response;
}

But i'm still getting

Fatal error: Uncaught Kohana_Request_Exception [ 0 ]: Unable to find a route to match the URI: test ~ SYSPATH\classes\kohana\request.php [ 674 ] thrown in C:\Xampp\htdocs\system\classes\kohana\request.php on line 674

instead of my custom 404 page. And yes, Kohana::$environment is set to Kohana::PRODUCTION;

It doesn't even get to the echo "ok"; part. Why doesn't the exception get caught?


回答1:


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;



回答2:


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>



回答3:


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



来源:https://stackoverflow.com/questions/4463862/how-to-setup-a-custom-404-page-for-a-kohana-v3-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!