在此处详述:Need assistance with Kohana 3 and catch all route turning into a 404 error作为接受的问题答案,我试图捕获Kohana抛出的错误,以显示相当错误的页面并发送正确的HTTP代码。在执行期间在Kohana v3中捕获Kohana_Request_Exception - 意外的行为
这里是一个简化的版本来演示该问题:
try {
// Instantiate your Request object
$request = Request::instance();
// The give it a try, to see if its a valid request
$request->execute();
}
catch (Kohana_Request_Exception $e) {
header('Content-Type: text/html; charset='.Kohana::$charset, TRUE, 404);
echo Request::factory('err/404')->send_headers()->execute()->response;
exit;
}
echo $request->send_headers()->response;
所以我浏览到一个不存在的URL,如http://example.local/moo/,我得到如下回应
Kohana_Request_Exception [ 0 ]: Unable to find a route to match the URI: moo
这里是什么happening- - 正在尝试请求,因Kohana_Request_Exception失败,它被捕获但是当我尝试构建一个新的请求对象时,Request::factory('err/404')
该请求抛出错误f从我的第一个请求....!? WTF?
我已经把它弄了好一个小时,而且和我刚开始时一样困惑。不应该来自工厂的新请求不知道旧请求吗?为什么当我从d00d的答案中实际复制代码时,此代码出现故障?
// Release version and codename
const VERSION = '3.0.7';
const CODENAME = 'hattrick';
有人指出我在正确的方向.. thx家伙。
不要直接调用`header()`。它只会被Request :: send_headers()。覆盖。改为使用Request-> headers []`和`Request-> status`。 – shadowhand 2010-12-10 09:29:45