Richard Holloway Blog

Redirect to 404 page in a Zend Framework 2 controller

Posted in July 2012 by under zend-framework

In Zend Framework 1, when you want to redirect to a 404 page, you can throw an exception like this

throw new Zend_Controller_Action_Exception('404 Page not found',404);
            

Or you can set the response code

$this->getResponse()->setHttpResponseCode(404);
            

In Zend Framework 2, rather than throw an exception, you can set a 404 status code on the reponse object, which will trigger the RouteNotFoundStrategy

$this->getResponse()->setStatusCode(404);
return;
            

I am not sure if this is documented anywhere but I found the information in this discussion.

Update - October 2013

You can now use

return $this->notFoundAction();