10

I have a controller called articles, which creates the articles model which gets the relevant data from the database.

I want to, if the method I call returns false, to trigger a 404 error. This is what I have so far.

 $articleName =  $this->uri->segment('articles');

 $article = new Articles_Model();

 $data = $article->getArticleUsingSlug($articleName);

 if (!$data) {
    Kohana::show_404; // This doesn't work.
 }

I just added my own custom hook which redirects the user to an actual 404 (/articles/page-not-found/) as triggered by Kohana, but is there a way I can invoke its internal 404 method to make Kohana give up processing my controller and use my new hook ?

hakre
  • 193,403
  • 52
  • 435
  • 836
alex
  • 479,566
  • 201
  • 878
  • 984

3 Answers3

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]]);
Sampson
  • 265,109
  • 74
  • 539
  • 565
11

This works for me:

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

What version of Kohana are you using?

pifantastic
  • 861
  • 6
  • 13
  • 2
    Also note that if $config['display_errors'] = TRUE; you will see a stack trace along with your 404 page. If you want JUST a 404 page to display set display_errors = FALSE – pifantastic May 06 '09 at 00:17
1

Kohana Docs tells you way to do it:

throw HTTP_Exception::factory(404, 'File not found!');
Norman Edance
  • 352
  • 4
  • 14