Installing PHPUnit

The book's examples use PHPUnit for testing. You will need PHPUnit installed to run these tests. PHPUnit can be installed using PHP's PEAR interface and using the following commands pear channel-discover pear.phpunit.de pear install --alldeps phpunit PHPUnit Running this command will automatically install PHPUnit for you. The version used in the book is 3.3.6 although later 3.x versions should also work. You can find more information about PHPUnit at http www.phpunit.de. To test your...

ZendControllerRouterRouteHostname

Router_Route_Hostname unsurprisingly handles hostname routing. A common use case for this is matching usernames that are in the sub domain segment of the hostname. For instance, if we have our public facing web site on www.domain.com and our registered users have account URL like user1.domain.com, then we can use the Router_Route_Hostname route to rewrite the requests to the account controller. route new Zend_Controller_Router_Route_Hostname , username.domain.com', array 'controller' gt...

Standard caches

Some of the Zend Framework components also provide ways of caching parts of them to improve performance. These standard caches can dramatically improve the performance of the components. The components that have caches are All of these components can use caching to improve their performance. We are not going to cover all of them, but generally these components will provide a setCache method or something similar that can be used to pass a zend_Cache instance to the component. This will then be...

Services for cross module communication

When we are using multiple modules, it is very likely that we will need to share the functionality stored in the Models between each module. With Zend_Application, this is very easy as all we need do is instantiate the other module's Models. For example, if we are inside the Storefront module and want to access the Cms module's Models, then we can simply do When we instantiate the Model from within the Storefront module, the autoloader will automatically include the class for us. However, as...

Creating the bootstrap resources

So far we have only created a basic skeleton for our Bootstrap class. Let's add the functionality to our three Bootstrap class resources now. The Locale resource configures the applications locale. This is always a good practice when creating an application as many Zend Framework components are locale aware. To set the locale, we simply create a new Zend_Locale instance and set it in the registry using the string Zend_Locale. This will then be accessible by all the locale aware components....

Creating the Cart Controller

With our Model and Model Resources created, we can now start wiring the application layer together. The Cart will have a single Controller, cartcontroller that will be used to add, view, and update cart items stored in the Cart Model. class Storefront_CartController extends Zend_Controller_Action protected _cartModel protected _catalogModel this- gt _cartModel new Storefront_Model_Cart this- gt _catalogModel new Storefront_Model_Catalog product this- gt _getParam 'productid' if null product...

CatalogController

The CatalogController is our main Controller and contains the majority of our control logic for the storefront currently and is responsible for the listing and viewing of products. To start, let's create the basic structure and add each action one at a time. class Storefront_CatalogController extends Zend_Controller_Action this- gt _catalogModel new Storefront_Model_Catalog This is our basic controller structure. We have a protected the _catalogModel property and a single init method. The init...

Model Resource implementation

Now that our resources are implementing their respective interfaces, we need to implement all the methods defined by them. Let's go through and add these now. We will explain the functionality as we go. To start, we will create the getCategoriesByParentId method. public function getCategoriesByParentId parentId - gt where 'parentId ', parentId - gt order 'name' We will need to be able to fetch all categories within a category. We do this by getting all records that match the passed in parentId...

Headtitle 302 Found

bootstrap abstract class 95 .htaccess 16 __get method 142 __isset method 142 _forward utility method 27 _getProductForm method 281 _getShippingMultiOptions method 236 _redirect utility method about 29 arguments 29 implementing, in MVC 253 unit testing 264, 265 ACL, in Domain layer advantages 255 disadvantages 255 ACL action helper about 283, 284 admin function, securing 286 init hook 284 postDispatch hook 284 preDispatch hook 284 ACL, in Domain layer 255 centralized global ACL, using 254 Module...

Modules controllers and actions

A major part of the Front Controllers responsibility is to help with the configuration of modules, controllers, and actions. In order to work correctly, the Front Controller needs to know where we have placed our controllers and how they are organized. In our Hello Zend application, we used the most basic directory structure available in the Zend Framework. controllers models Q views i l library Q HZend Zend LU public Using this structure, all of our controllers, models, and views are held in...

Fat Model Skinny Controller

Along with our original question of where we should put our code, there are also many questions about whether code should reside in either the Controller layer or the Domain layer. This is where the concept of the Fat Model Skinny Controller comes in. The idea here is that we should try to push as much logic as possible into our Domain layer. By doing this, we hope to achieve the following goals Enhance separation of concerns

The bootstrap class

To complete our basic bootstrapping, we need to create the bootstrap class for class Bootstrap extends Zend_Application_Bootstrap_Bootstrap protected function _initViewSettings To create a bootstrap class for zend_Application, we must subclass the zend_Application_Bootstrap_Bootstrap class. This class contains all the base functionality required for our bootstrap class. We call the class Bootstrap. This is the default name that zend_Application will look for when loading the bootstrap file. We...

Bootstrapping modules

Now that we have enabled the Modules Resource plugin, it will try to load a bootstrap class for each of our modules. Therefore, we need to create a new bootstrap class for the cms module. Create the following class within the cms module folder class Cms_Bootstrap extends Zend_Application_Module_Bootstrap public function _initModuleResourceAutoloader 'modelResource' gt array 'path' gt 'models resources', The new Cms_Bootstrap class will now be loaded and executed during the bootstrap process....

ZendApplication configuration

In our application.php.dist file, we have opted to use Zend_Config to define the options we send to the Zend_Application instance. All options for the application will be stored within the store.ini in the config directory. Autoloadernamespaces Zend_ Autoloadernamespaces SF_ phpsettings.display_errors 0 phpsettings.error_reporting 8191 phpsettings.date.timezone Europe London bootstrap.path APPLICATION_PATH modules resources.frontcontroller.defaultmodule storefront true...

ACL action helper

Rather than duplicating our ACL querying code in each Controller, we are going to create an Action Helper to encapsulate this for us. Create the following class in the SF library class SF_Controller_Helper_Acl extends protected _acl protected _identity module acl ucfirst module . '_Model_Acl_' . ucfirst module if class_exists acl this- gt _acl new acl public function isAllowed resource null, privilege null if null this- gt _acl return null return public function setIdentity identity if isset...