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

ale*_*lex 10 php kohana http-status-code-404

我有一个名为的控制器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.
 }
Run Code Online (Sandbox Code Playgroud)

我刚刚添加了我自己的自定义钩子,它将用户重定向到由Kohana触发的实际404(/ articles/page-not-found /),但有没有办法可以调用其内部404方法让Kohana放弃处理我的控制器并使用我的新钩子?

pif*_*tic 11

这对我有用:

Event::run('system.404');
Run Code Online (Sandbox Code Playgroud)

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

  • 另请注意,如果$ config ['display_errors'] = TRUE; 您将看到堆栈跟踪以及404页面.如果你想要一个404页面来显示set display_errors = FALSE (2认同)

Sam*_*son 11

Kohana/General /错误处理/ Kohana_404_Exception

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