如何做到这一点?我正在尝试这样做大约半个小时,它变得非常烦人。你应该这是一个基本和简单的事情来设置这样的框架。我希望也许我错过了一个简单的方法,因为我开始的事情我不应该选择这个框架,如果这样的基本铃声很难设置。
这是在我的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;
}
但我还是得到了
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
而不是我的自定义404页面。
是的,Kohana::$environment
设置为Kohana::PRODUCTION;
它甚至没有进入echo "ok";
部分。为什么异常没有被抓住?
答案 0 :(得分: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;
创建新控制器“ 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
在“ kohana ”文件夹中创建两个文件( exception404.php 和 exception403.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
现在你可以手动抛出404和403错误(你不能抛出错误500;)
throw new Kohana_Exception404;
throw new Kohana_Exception403;
答案 1 :(得分:6)
您需要做的就是在bootstrap.php add中设置不同视图的路径:
Kohana_Exception::$error_view = 'error/myErrorPage';
将解析当前正在解析的所有变量到生成的错误页面:
system/views/kohana/error.php
即:
<h1>Oops [ <?= $code ?> ]</h1>
<span class="message"><?= html::chars($message) ?></span>
答案 2 :(得分:5)
从v3.1开始,Kohana指南提出了一个tutorial on custom error pages,它显示了一种更清晰的方法来解决这个问题。