2010-12-05 26 views
1

在此处详述: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家伙。

+0

不要直接调用`header()`。它只会被Request :: send_headers()。覆盖。改为使用Request-> headers []`和`Request-> status`。 – shadowhand 2010-12-10 09:29:45

回答

0

啊哈!感谢球员,但我得到它计算..我应该看看堆栈跟踪更近一点,错误页面的模板控制器的before()方法抛出一个新的错误,因为它试图检查使用Request::instance()验证我改变它为Request::current()它都是光泽。

感谢您的帮助!

0

这是因为在Request::factory('err/404')中,err/404部分也是试图与您的路由匹配的网址。它永远不会匹配,即你有一个路线有这样的事情...

Route::set('errors', 'err/<action>', array('action' => '404|500')) {} 

你也应该发送404通过......

$request->status = 404;