如何为Kohana v3应用程序设置自定义404页面

dan*_*els 5 php kohana-3

如何才能做到这一点?我正在尝试这样做大约半个小时,它变得非常烦人.你应该这是一个基本和简单的事情来设置这样的框架.我希望也许有一个我错过的简单方法,因为我开始的事情我不应该选择这个框架,如果这样的基本铃声很难设置.

这是在我的bootstrap.php文件中应该做的伎俩.

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;
}
Run Code Online (Sandbox Code Playgroud)

但我还是得到了

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
Run Code Online (Sandbox Code Playgroud)

而不是我的自定义404页面.是的,Kohana::$environment设置为Kohana::PRODUCTION;

它甚至没有达到这一echo "ok";部分.为什么没有抓住异常?

Kon*_*zov 7

bootstrap.php的最后一行替换为:

/**
* 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;
Run Code Online (Sandbox Code Playgroud)

创建新控制器" 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
Run Code Online (Sandbox Code Playgroud)

在" kohana "文件夹中创建两个文件(exception404.phpexception403.php):

<?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
Run Code Online (Sandbox Code Playgroud)

现在你可以手动抛出404和403错误(你不能抛出错误500;)

throw new Kohana_Exception404;
throw new Kohana_Exception403;
Run Code Online (Sandbox Code Playgroud)

  • 如果像我一样,你阅读本页并使用Kohana 3.2(或更高版本),你可能不得不用`HTTP_Exception_404`替换`Kohana_Exception404`. (6认同)

mds*_*ner 6

您需要做的就是在bootstrap.php add中设置不同视图的路径:

Kohana_Exception::$error_view = 'error/myErrorPage';
Run Code Online (Sandbox Code Playgroud)

这将解析当前正在解析的所有变量到生成的错误页面:

system/views/kohana/error.php
Run Code Online (Sandbox Code Playgroud)

即:

<h1>Oops [ <?= $code ?> ]</h1>
<span class="message"><?= html::chars($message) ?></span>
Run Code Online (Sandbox Code Playgroud)


Let*_*rgy 5

从v3.1开始,Kohana指南提供了关于自定义错误页面教程,页面显示了解决此问题的更清晰的方法.