In our previous session of Zend Framework we learnt about concept of Autoloading, today let us go through the error handling in Zend Framework.
- In zend framework a Front Controller is the first which receives the requests, as a result, all the exceptions arrive to front controller only allowing the developer to handle the error there itself.
- By default, the Zend_Controller_Front aggregates the exceptions in the response object.
- Several mechanisms are already built in MVC to handle errors:
- By default the error handler plugin is registered and used. This plugin is designed to handle,
- Errors due to missing controllers or actions
- Errors occurring within action controllers
- Exceptions can also be handled by a function throwExceptions() of Zend_Controller_Front.
- If we pass a Boolean TRUE value in this throwExceptions() method, we can tell the front controller that instead to gathering all the exceptions in the response object or using the error handler plugin, we would rather handle them ourselves, as shown below using try- catch:
$fr->throwExceptions(true); try { $fr->dispatch(); } catch(Exception $s) { //handle exception here }
$fr->returnResponse(true); $resp = $fr->dispatch(); if ($resp->isException()) { $excep = $resp->getException(); // handle exceptions here } else { $resp->sendHeaders(); $resp->outputBody(); }
$fr->throwExceptions(false);
class SomeController extends Zend_Controller_Action { public function someAction() { if($this->_getParam(‘parameter’,false)) { //parameter present } else { //parameter not present throw new ParameterNotFoundException(Parameter is missing!'); } } }
Thus we studied about error/exception handling in this Error Handling in Zend Framework tutorial.