Zend Controller Error Handling - Zend

So far, I have covered everything from the controller to the view in a world in which there are no deadlines, the coffee flows from trees, magical fairies grant you wishes, and the code always works. Then you realize you don’t live in that world—the code has bugs or(worse) it breaks the application. In this section, you’ll use all the assets you previously learned to set up the error handlers by creating userfriendly error messages and handling.

When I’m in a hurry, I tend to type in a URL extremely quickly. I have come up with more than 100 ways to type in Google, for instance, and more than 20 ways to type in my full name. When it comes to the incorrectly typed-in URL, I get sent to a page I wasn’t expecting or to a broken page. As far as you’re concerned, a broken or blank page is PHP’s way of telling you that there was a fault in the script or the user requested a bad URL.

It’s your job to give the user a seamless experience when visiting the web application, so a blank page won’t do. Zend Framework has introduced the ability to handle the behavior of controlling what users see when they encounter an error in the application by allowing you to turn on (or off) error/exception handling within the application. This functionality is on by default.

Let’s create a generic error handler. You want to avoid showing the user a blank page or a long PHP error detailing at what line the error occurred. The default exception handler contains three parts because, like all components in Zend Framework, it is also treated like a page request. So you’ll need a controller, an action in the controller, and an HTML page to render all errors or any default message you want to display to the user.

Open the application/controllers directory and open the file called ErrorController.php, which Zend Framework uses as the default error controller. This file contains a single action that handles any errors the user might come across, as shown in Listing.

ListingError Handler Controller: ErrorController.php

<?php
class ErrorController extends Zend_Controller_Action {
public function errorAction(){}
}

After you have the controller, go to the views/scripts/error directory and save the markup shown in Listing into error.phtml.

ListingError Handler View: error.phtml

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>LoudBite.com</title>
</head>
<body>
We're sorry, you have located a page that does not exist or contains an error.
</body>
</html>

The controller contains only one default action that is called automatically by Zend Framework every time the user encounters an error. The HTML file contains the message that you want to display to users when they encounter an error. In this case, it is a simple message.

Extending Error Handling

Zend Framework allows you to take full control of error handling and drill down into key messages created by the framework for debugging purposes. If you don’t want to display an error message, but want to simply allow the script to crash, that can be done. If you want to trace any messages for better error handling, that also can be done.

You can set the name of the controller that handles errors using built-in functions such as setErrorHandlerModule(), setErrorHandlerController(), setErrorHandlerAction(), and set Error Handler (). Let’s take a look at a few examples of manipulating the error handler controller within the front controller.

Setting a Different Error Handler

To set a different error handler, update the public/index.php file and add in a few more lines to allow the front controller to forward any errors to the new error handler (in this case, ApplicationError). You don’t have to add the Controller suffix to the name because Zend Framework automatically does this for you. You also forward the user to indexAction within the ApplicationErrorController instead of errorAction. This is all done using the Zend_ Controller_ Plugin_ErrorHandler object’s setErrorHandlerController() and set Error Handler Action () setters.

Listing Updated FrontController in index.php: Using Setters

$FrontController = Zend_Controller_Front::getInstance();
$plugin = new Zend_Controller_Plugin_ErrorHandler();
$plugin->setErrorHandlerController("ApplicationError");
$plugin->setErrorHandlerAction("index");
$FrontController->registerPlugin($plugin);

Another simple way to condense the preceding code into a single line is to use the setErrorHandler() function that accepts a key-value pair array.

Listing Updated Front Controller

$FrontController = Zend_Controller_Front::getInstance();
$plugin = new Zend_Controller_Plugin_ErrorHandler();
$plugin->setErrorHandler(array("controller" => 'ApplicationError', "action" => 'index'));
$FrontController->registerPlugin($plugin);

Using Error Variables in the View

Another exciting feature of Zend Framework error handling is the capability to display internal Zend Framework errors that occur while rendering a page or calling a controller-action pair. To do this, you need to modify the controller that handles the errors. If you follow the preceding code, it will be the ApplicationErrorController.php file.

In the indexAction() function, make the adjustments shown in Listing.

Listing Application Error Handler: indexAction()

public function indexAction(){
//Get the controller's errors.
$errors = $this->_getParam('error_handler');
$exception = $errors->exception;
//Initialize view variables.
$this->view->exception = $exception;
}

The new lines capture all the controller’s internal error messages and place them into the $exception variable. By utilizing _getParam(), you can retrieve any errors encountered from the current request object by passing the error_handler parameter. This parameter contains more information than you might need, but the exception attribute is most important for now. After you have the exception attribute information stored in its own object, you can do a lot with it. For starters, you can display the internal Zend Framework error message using getMessage(), as you do in Listing, or you can display the steps that Zend Framework took to get to the current error using getTrace().

The following line is required if you want to display the error in the .phtml file by making the exception object available there:

$this->view->exception = $exception;

Take a look at the functions shown in Table; Zend Framework provides you with a lot of useful information. You can use these functions to display the information stored in the exception object.

Zend_Exception Operations

Listing shows how to use the getMessage() function to get details of the exception in the view. This file is index.phtml in views/scripts/application-error/.

Listing index.phtml:Using getMessage()

<html>
<head>
<title>Untitled</title>
<link rel="stylesheet" type="text/css" href="my.css">
</head>
<body>
Whoops you encountered the below error.
<?php echo $this->exception->getMessage();?>
</body>
</html>

Because you have initialized the variable in the controller to use in the view, you can call up all functions available to you. If you want to display the trace, you can easily change the getMessage() call to getTrace() and print out the variable in your own way.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Zend Topics