Zend Framework Quick Start
Create an Error Controller & View
The last things you want to display to an end user of your site are either a blank page or exceptions. Zend Framework's MVC provides an ErrorHandler plugin that will detect exceptions and dispatch an ErrorController so that you can easily display appropriate content to your end user.
Additionally, you need some way to handle 404 errors- i.e., situations when the action or controller are not found.
In development, you do want to see exceptions, however. Fortunately, ZF MVC can accomodate both situations.
The ErrorHandler plugin by default will dispatch the errorAction() of the ErrorController. Let's build our controller.
<?php
// application/controllers/ErrorController.php
/**
* ErrorController
*/
class ErrorController extends Zend_Controller_Action
{
/**
* errorAction() is the action that will be called by the "ErrorHandler"
* plugin. When an error/exception has been encountered
* in a ZF MVC application (assuming the ErrorHandler has not been disabled
* in your bootstrap) - the Errorhandler will set the next dispatchable
* action to come here. This is the "default" module, "error" controller,
* specifically, the "error" action. These options are configurable.
*
* @see http://framework.zend.com/manual/en/zend.controller.plugins.html
*
* @return void
*/
public function errorAction()
{
// Ensure the default view suffix is used so we always return good
// content
$this->_helper->viewRenderer->setViewSuffix('phtml');
// Grab the error object from the request
$errors = $this->_getParam('error_handler');
// $errors will be an object set as a parameter of the request object,
// type is a property
switch ($errors->type) {
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;
default:
// application error
$this->getResponse()->setHttpResponseCode(500);
$this->view->message = 'Application error';
break;
}
// pass the environment to the view script so we can conditionally
// display more/less information
$this->view->env = $this->getInvokeArg('env');
// pass the actual exception object to the view
$this->view->exception = $errors->exception;
// pass the request to the view
$this->view->request = $errors->request;
}
}
The above may seem a bit esoteric.
First, the ErrorHandler plugin stores error information- including the error type, exception caught, and the request that produced it- within a token that is passed in the request. We store that in the $errors variable.
Second, we do some switching based on the exception type. There are two types of exceptions that represent 404 errors: controller not found and action not found. For these, we change the HTTP response code to 404 to indicate this. All other exceptions are considered application exceptions, and we'll return the appropriate HTTP response code for these- 500.
Third, remember the env variable you passed to the front controller in the bootstrap file? We're going to get that from the front controller and pass it to the view. More on this later.
Finally, we pass the exception and request to the view.
Let's look at the related view script, which we'll place in application/views/scripts/error/error.phtml.
<? // application/views/scripts/error/error.phtml ?>
<h1>An error occurred</h1>
<h2><?= $this->message ?></h2>
<? if ('development' == $this->env): ?>
<h3>Exception information:</h3>
<p>
<b>Message:</b> <?= $this->exception->getMessage() ?>
</p>
<h3>Stack trace:</h3>
<pre><?= $this->exception->getTraceAsString() ?></pre>
<h3>Request Parameters:</h3>
<pre><? var_dump($this->request->getParams()) ?></pre>
<? endif ?>
We're now using the env variable in the view script to determine whether or not to display exception information. In development, you'll get all the information for debugging. Changing the value in your bootstrap.php file will prevent this sensitive information from being displayed, while providing a reasonable error message to your end user.
Quickstart Navigation
- ZF & MVC Introduction
- Set Up the Project Structure
- Download & Install ZF
- Create a Rewrite Rule
- Create a Bootstrap File
- Create an Action Controller & View
- Create an Error Controller & View
- Create a Layout
- Create a Configuration and Registry
- Create a Model and Database Table
- Create a Form
- Congratulations
ZF Reference Guide - now in PDF!
The Zend Framework Reference Guide is now available in PDF format from Zend's
high-speed content distribution network. Registration is required.
Documentation Archives
If you're looking for an older version of our reference guide, you'll find it in our download archives.
