Zend Controller Error Plugin and 404’s


The Zend Framework provides a nice plugin to handle errors that bubble up in your controllers. It will automatically route Exceptions to the Error Controller, assuming you have flagged the Front Controller to not display exceptions (which should be the case in a production site). I implemented code for handling the Error Controller that was similar to the code found in this posting. But then I couldn’t figure out how to trigger the switch statement for the no action exception (i.e. Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION). Whenever I threw a Zend_Controller_Action_Exception it went to the default case, for which I had a message saying a server error occurred.

Digging into Zend_Controller_Plugin_ErrorHandler, I found I needed to put a 404 code into the constructor. So now my code looks like:


<?php
throw new Zend_Controller_Action_Exception(‘This page dont exist’,404);

?>


Zend_Controller_Plugin_ErrorHandler now sets the type as EXCEPTION_NO_ACTION. Now I can throw that when I need a 404 Not Found page. It isn’t the cleanest approach, since I am throwing that in a method, and technically I do have an action. But that seems to be the easiest way I have found, rather than forwarding the request to the Error Handler.

One Response to “Zend Controller Error Plugin and 404’s”

  1. 404 Pages Should Not Return 200 Status Codes | Hobby Cash: Make Cash Blogging About the Things You Love Says:

    […] Zend Controller Error Plugin and 404’s […]

Leave a Reply