In Zend Framework applications, error handling is usually covered by the error controller. However, there is acouple of different ways how programmer can send the execution to it. The big releif ad programmer's tiem saving comes from the fact that there is not much to error handling in ZF thanks to proprely though through and ready components that make it really easy.
In short: just throw an exception, and write some code in the error controller to handle it. It is also good practice to log the exceptions that get as far as the error controller.
In order to enable the expected behaviour of error controller, you need to set throwExceptions to false in the front controller:
The best place for setting throwExceptions to false would be Bootstrap file.
In Zend Framework v 1.10.3 this befaviour is enabled by default.
Let’s look at differnet ways of triggerring the error controller when you have error condition in your application.
The simpliest way of raising exceptions is the following:
Here is a full example:
public function yourAction() {
if($this->_getParam('foo',false)) {
//Success: parameter exists
}
else {
//Logical Error: parameter not present
$this->_forward('someerror', 'error');
}
}
}
All you have to do now is to create public function someerrorAction in ErrorController and respective phtml view.
N.B.! _forward does not work in public function init(). if you need to use in init(), please create public function preDispatch() (which will be executed after init() ) and call _forward in preDispatch() function.
The probelm of this simple approach is that it goes against best practicies, specificly it creats coupling between YourController and ErrorController.
So the best option will be be just ot through exception like this:
PHP and Zend Framework will do all the magic for you and excpetion execution will end up in ErrorController!
Now when excptional program path is captured in ErrorController, you might want to diplay different messages on the screen or execute some specific logic based on what exception being thrown. So, to do that you need just to create a special case for you exception in ErrorController like shown in the example below:
public function errorAction()
{
$errors = $this->_getParam('error_handler');
switch ($errors->type) {
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
// 404 error -- controller or action not found
$this->getResponse()->setHttpResponseCode(404);
$this->view->message = 'Page not found';
break;
// ATTENTION: here it is:
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_OTHER:
$this->view->message = 'OTHER ERROR';
switch ($errors->exception->getMessage()) {
// set your exception message here:
case 'someerror':
//do stuff
break;
default:
break;
}
break;
default:
// application error
$this->getResponse()->setHttpResponseCode(500);
$this->view->message = 'Application error';
break;
}
...
}
}
public function pageNotFoundAction() {
//goes here if the page was not found
}
public function notAuthorizedAction() {
//goes here if the user has no access to a page
}
}
This is pretty much it. Easy!