Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1
CHAPTER 15 ■ INTRODUCTION TO THE ZEND FRAMEWORK^219

front controller, and then call the throwExceptions() method, before finally calling run with
your controller directory, as follows:

$front = Zend_Controller_Front::getInstance();
$front->throwExceptions(true);
$front->run(APP_PATH. '/application/controllers');

Before you launch your site, be sure to disable the throwing of exceptions, as it is an important
security measure.
Completed, your bootstrap file should look like Listing 15-1.

Listing 15-1. A Zend Framework Bootstrap File (index.php)

<?php
define('ZFW_VERSION','1.0.3');
define('ZFW_PREFIX','/usr/share/php/ZendFramework');
define('APP_PATH', realpath(dirname(__FILE__). '/../'));

$paths = array(
APP_PATH,
APP_PATH. DIRECTORY_SEPARATOR. 'application'. DIRECTORY_SEPARATOR. 'models',
ZFW_PREFIX. DIRECTORY_SEPARATOR. 'ZendFramework-'. ZFW_VERSION. ➥
DIRECTORY_SEPARATOR. 'library',
get_include_path()
);

set_include_path(implode(PATH_SEPARATOR, $paths));

require_once('Zend/Loader.php');

Zend_Loader::registerAutoload();

$front = Zend_Controller_Front::getInstance();
$front->throwExceptions(true);
$front->run(APP_PATH. '/application/controllers');

If you were to revisit your site now, you would see an exception that no index controller
exists. To clear this exception, you need to create your first Zend Framework action controller.

Creating Controllers, Views, and Models


Since the Zend Framework is designed for MVC development, it expects those three components:
a model, view, and controller. So, let’s create those components, starting with the controller.

Adding an Index Controller.


All controllers in the Zend Framework extend a common abstract class called Zend_Controller_
Action and must have a class name ending in Controller. The default action that will be called

McArthur_819-9C15.fm Page 219 Thursday, February 28, 2008 7:44 AM

Free download pdf