在Kohana,你能触发404错误吗?
-
05-07-2019 - |
题
我有一个名为 articles
的控制器,它创建了从数据库中获取相关数据的文章模型。
我想,如果我调用的方法返回 false
,则触发404错误。这是我到目前为止所做的。
$articleName = $this->uri->segment('articles');
$article = new Articles_Model();
$data = $article->getArticleUsingSlug($articleName);
if (!$data) {
Kohana::show_404; // This doesn't work.
}
我刚刚添加了我自己的自定义钩子,它将用户重定向到由Kohana触发的实际404(/ articles / page-not-found /),但有没有办法可以调用其内部404方法来使Kohana给出处理我的控制器并使用我的新钩子?
解决方案
这对我有用:
Event::run('system.404');
您使用的是什么版本的Kohana?
其他提示
Kohana / General /错误处理/ Kohana_404_Exception
/**
* @param string URL of page
* @param string custom error template
*/
throw new Kohana_404_Exception([string $page [, string $template]]);
Kohana Docs 告诉你要做的事情它:
throw HTTP_Exception::factory(404, 'File not found!');
不隶属于 StackOverflow