Zend Framework 2: Redirect to 404 page in Controller
I’ve been getting into trouble for several hours with redirecting to the 404 page in Zend Framework 2.
Note: Zend Framework 2 has been deprecated. Zend Framework was migrated to the Laminas Project in 2020, where development continues today. The
notFoundAction()technique below still applies in laminas-mvc.
I asked about this on Stack Overflow.
What Didn’t Work
Before that, I was using the following code:
$this->getResponse()->setStatusCode(404);
return;
This sets the HTTP status code to 404, which looks fine at first. But it only changes the response header; it doesn’t dispatch the error controller, so once I built a custom 404 page I found that it never rendered.
The Solution
To actually render the 404 error page, return notFoundAction() instead:
return $this->notFoundAction();
notFoundAction() is provided by AbstractActionController. It sets the 404 status and hands off to the configured error template, so your custom page is displayed.
The Disqus comment system is loading ...
If the message does not appear, please check your Disqus configuration.