在Kohana,你能触发404错误吗?

问题描述 投票:10回答:3

我有一个名为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放弃处理我的控制器并使用我的新钩子?

php kohana http-status-code-404
3个回答
11
投票

这对我有用:

Event::run('system.404');

您使用的是什么版本的Kohana?


11
投票

Kohana / General / Error-handling / Kohana_404_Exception

/**
 * @param  string  URL of page
 * @param  string  custom error template
 */
throw new Kohana_404_Exception([string $page [, string $template]]);

1
投票

Kohana Docs告诉你如何做到这一点:

throw HTTP_Exception::factory(404, 'File not found!');
© www.soinside.com 2019 - 2024. All rights reserved.