Pages

Monday, November 30, 2009

Forcing a 404 Error with Zend Framework

There are many things I like about Zend Framework. And there are times when using it is like searching for a black cat in a dark room. Seemingly simple tasks end up frittering away time looking through the documentation and source code.

Inducing a 404 response is one of these things. I didn't want to simply redirect the page, but rather, I wanted the ErrorController to handle the error just like usual.

In your action controller:

// Some "not found" condition occurs.
throw new Zend_Controller_Action_Exception('Page not found.',404);

And in views/scripts/error/error.phtml:

<? if (404 == $this->exception->getCode()): ?>
// Put your 404 message here.
<? endif ?>

1 comment: