Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1
CHAPTER 16 ■ ADVANCED ZEND FRAMEWORK^239

Next, you will need to determine why the exception was thrown. The error information is
contained in the request parameter 'error_handler' and can be fetched as shown in Listing 16-5.

Listing 16-5. Fetching Error Information (ErrorController.php)

<?php
class ErrorController extends Zend_Controller_Action {

public function errorAction() {
$request = $this->getRequest();
$errorHandler = $request->getParam('error_handler');

Zend_Debug::dump($errorHandler);
}
}

Finally set the throwExceptions() call in your bootstrap file to false, or simply remove the
entire line to re-enable the default behavior.
Now, if you generate an error, such as by visiting a nonexistent URL, you will get pages of
information about the error.
The error controller is not intended to catch all errors, like syntax errors, but it will do a
reasonably good job of catching any exception that occurs within your action controller’s
execution.

Application Logging


Zend_Log allows your applications to generate and record information about their operation.
This information can be integrated with error handling or used to generate debugging infor-
mation about operational parameters.
Zend_Log has four architectural parts:

Logs: These are the responsible objects, where you will call methods to add messages to
the log. They are responsible because they are where the other components tie in.

Writers: These allow you to record the data in a log to a variety of output formats— from
files and streams to a database when integrated with Zend_Db. I recommend using files
over recording events to a database, as database logging can make debugging a database
connection failure much more difficult.

Formatters: These are used to take the Zend_Log data and turn it into a specific format.
They are required because, by default, Zend_Log data is stored in an array format. Formatters
take this array form and turn it into a string for logging.

Filters: These allow you to control which error messages will be logged by defining a priority
threshold for your application. You may want to integrate the filter threshold with your
Zend_Config file.

These four components work together to provide a very powerful set of logging options. To
demonstrate the logging functionality, let’s look at how you can integrate the ErrorController

McArthur_819-9C16.fm Page 239 Friday, February 29, 2008 5:07 PM

Free download pdf