问题描述
基于此文档,我已经实现了捕获所有路由,该路由会路由到错误页面.
Based on this documentation, I've implemented a catch all route which routes to an error page.
这是我bootstrap.php
Route::set('default', '<path>', array('path' => '.+'))
->defaults(array(
'controller' => 'errors',
'action' => '404',
));
但是,当我尝试进入一个不存在的页面时,却不断抛出此异常
However I keep getting this exception thrown when I try and go to a non existent page
如果我将<path>
段设为可选(即用括号括起来),那么它似乎只是加载了home
路由,即...
If I make the <path>
segment optional (i.e. wrap it in parenthesis) then it just seems to load the home
route, which is...
Route::set('home', '')
->defaults(array(
'controller' => 'home',
'action' => 'index',
));
首先定义归属路线.
我这样执行我的主要请求
I execute my main request like so
$request = Request::instance();
try {
// Attempt to execute the response
$request->execute();
} catch (Exception $e) {
if (Kohana::$environment === Kohana::DEVELOPMENT) throw $e;
// Log the error
Kohana::$log->add(Kohana::ERROR, Kohana::exception_text($e));
// Create a 404 response
$request->status = 404;
$request->response = Request::factory(Route::get('default')->uri())->execute();
}
$request->send_headers();
echo $request->response;
这意味着404标头已发送到浏览器,但是我假设通过将请求发送到捕获所有路由,那么它应该显示在我的错误控制器中设置的404错误.
This means that the 404 header is sent to the browser, but I assumed by sending the request to the capture all route then it should show the 404 error set up in my errors controller.
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Errors extends Controller_Base {
public function before() {
parent::before();
}
public function action_404() {
$this->bodyClass[] = '404';
$this->internalView = View::factory('internal/not_found');
$longTitle = 'Page Not Found';
$this->titlePrefix = $longTitle;
}
}
为什么不显示我的404错误页面?
Why won't it show my 404 error page?
推荐答案
对于我的项目,我从未为404指定任何特定的分隔路线.当找不到合适的路线时,我只是捕获了由路线引发的异常.
For my projects I never specified any specific separated route for 404. I just catch exceptions thrown by routing, when no appropriate route was found.
这是我如何处理在Kohana中发现的所有异常的方法.希望对您有帮助.
Here is how I am handling all kind of exceptions that I've discovered in Kohana. I hope it will help you.
try
{
try
{
$request = Request::instance();
$request->execute();
}
catch (ReflectionException $e)
{
Kohana::$log->add(Kohana::ERROR_404, Kohana::exception_text($e));
if (!IN_PRODUCTION)
{
throw $e;
}
$request->response = Request::factory('err/404')->execute();
}
catch (Exception404 $e)
{
Kohana::$log->add(Kohana::ERROR_404, Kohana::exception_text($e));
if (!IN_PRODUCTION)
{
throw $e;
}
$request->response = Request::factory('err/404')->execute();
}
catch (Kohana_Request_Exception $e)
{
Kohana::$log->add(Kohana::ERROR_404, Kohana::exception_text($e));
if (!IN_PRODUCTION)
{
throw $e;
}
header('Content-Type: text/html; charset='.Kohana::$charset, TRUE, 404);
echo Request::factory('err/404')->send_headers()->execute()->response;
exit;
}
catch (exception $e)
{
Kohana::$log->add(Kohana::ERROR, Kohana::exception_text($e));
if (!IN_PRODUCTION)
{
throw $e;
}
$request->status = 500;
$request->response = Request::factory('err/500')->execute();
}
}
catch (exception $e)
{
if (!IN_PRODUCTION)
{
throw $e;
}
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
here is the message that everything is veeeery bad
</body>
</html>';
exit;
}
Exception404:
Exception404:
class Kohana_Exception404 extends Kohana_Exception
{
public function __construct($message = 'error 404', array $variables = NULL, $code = 0)
{
parent::__construct($message, $variables, $code);
}
}
这篇关于需要有关Kohana 3的帮助,并捕获所有变成404错误的路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!