Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1
CHAPTER 17 ■ THE ZEND FRAMEWORK APPLIED^261

Once you have a library function, you need to add your library directory to the included
paths in the application’s bootstrap. Listing 17-3 demonstrates how to modify your bootstrap file.

Listing 17-3. Bootstrapping a Custom Library (index.php)

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

$paths = array(
APP_PATH,
ZFW_PREFIX. DIRECTORY_SEPARATOR. 'ZendFramework-'. ZFW_VERSION. ➥
DIRECTORY_SEPARATOR. 'library',
YourPrefix_PREFIX. DIRECTORY_SEPARATOR. 'library',
get_include_path()
);
set_include_path(implode(PATH_SEPARATOR, $paths));

require_once('Zend/Loader.php');
Zend_Loader::registerAutoload();

$tax = YourPrefix_Tax_Calculator::calculate(1);

This approach depends entirely on you closely following the Zend Framework standard
class naming conventions. If you wanted to load a class like TaxCalculator without prefixes or
underscores, you would need to write your own loader. This can be achieved by either extending
Zend_Loader or by registering an additional spl_autoload function, as explained in Chapter 9.

The Request Cycle


Now that you have an understanding of modules and models, it’s time to get into the gears of
the Zend Framework and truly understand the steps the framework goes through when routing
and dispatching your application. Figure 17-1 illustrates the complete flow of a Zend Frame-
work application.
The first thing you’ll notice is that after a request is received, routing begins. (Ignore the
plug-in and helper brokers for now; they will be discussed in the next sections.) Routing is the
process by which the framework takes the requested URL and matches it to the list of rules speci-
fied to the router. These rules allow the router to dissect the URL into a module, controller, action,
and any number of defined parameters. All of this information is then stored in the Zend_
Request object and passed to the dispatching cycle.
Dispatching runs as a loop, allowing various mechanisms to reset the application and
send it back to dispatch start, such as to forward an unauthorized user to a login screen. The
dispatch process is handled by Zend_Controller_Dispatcher_Standard by default, which will
instantiate your action controller and result in a call to init(). Next, it calls dispatch() on the
action controller.

McArthur_819-9.book Page 261 Friday, February 29, 2008 8:03 AM

Free download pdf