Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1

(^260) CHAPTER 17 ■ THE ZEND FRAMEWORK APPLIED
./application/modules/admin/views
./application/modules/admin/views/scripts
./application/modules/admin/views/scripts/index
./application/modules/admin/models
./application/modules/admin/controllers
./document_root
To enable modular use, in your bootstrap file, simply point the front controller to your
modules directory:
$front->addModuleDirectory(APP_PATH. '/application/modules');
The index module is called 'default', and requests for / will forward to /default/index/
index. That’s all there is to using a modular setup with the framework.


Model Libraries and Zend_Loader


You will notice in the conventional setup that modules do not share models directories. This
can help separate out component logic and make modules easier to share among applications;
however, it can also lead to duplication.
Many framework developers find it useful to keep a reusable component library and to
package it in a similar way to how the framework itself is packaged. The advantage to this versus
application-level models is that you can share your library among applications and between
modules. The downside is that you cannot make assumptions about the application environ-
ment, such as registry objects or bootstrap settings. Any settings of this nature that you wish to
bind must be explicitly passed when your library is bootstrapped.
To add a model library, start by creating a directory called library somewhere global, such
as /usr/share/php/YourPrefix. This library directory is what will be passed as an include path
to the bootstrapping process.
The next part is to follow the naming convention very closely. Class names should all start
with a common prefix, and then follow with camel-cased words separated by underscores (_)
where you want a directory separation. For example, YourPrefix_Tax_Calculator would reside
in library/YourPrefix/Tax/Calculator.php. If you don’t wish to use this naming convention,
perhaps because you want to integrate an existing library, you will not be able to access your
library though the standard Zend_Loader autoloading mechanism.
Listing 17-2 shows how to create a library model.

Listing 17-2. Creating a Library Model (/usr/share/php/YourPrefix/library/YourPrefix/Tax/
Calculator.php)

<?php
class YourPrefix_Tax_Calculator {
public static function calculate($a) {
return $a * 1.07;
}
}

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

Free download pdf