List of Listings – Zend Framework in Action

List of Listings

Chapter 2. Hello Zend Framework!

Listing 2.1. The bootstrap file, index.php, initializes and runs the application

Listing 2.2. The bootstrap file: public/.htaccess

Listing 2.3. The index controller: application/controllers/IndexController.php

Listing 2.4. The view template: views/scripts/index/index.phtml

Listing 2.5. The FormatCurrency view helper

Listing 2.6. Automatically maintaining date fields in a model

Chapter 3. Building a website with Zend Framework

Listing 3.1. The initial Bootstrap class: application/bootstrap.php

Listing 3.2. An example Zend_Config INI file

Listing 3.3. The initial config.ini file for Places: application/config.ini

Listing 3.4. public/index.php is used to run the application

Listing 3.5. MySQL statements to create and populate the tables with initial data

Listing 3.6. A Places model unit test: tests/Models/PlacesTest.php

Listing 3.7. The TestConfiguration class

Listing 3.8. fetchLatest() ensures the business logic is kept in the model layer

Listing 3.9. The test case for the fetchLatest() method

Listing 3.10. indexAction() displays a list of recently added places

Listing 3.11. Testing the index controller

Chapter 4. Managing the view

Listing 4.1. Starting Zend_Layout in application/bootstrap.php

Listing 4.2. The Places_Controller_Plugin_ViewSetup front controller

Listing 4.3. The master layout script: view/layouts/layout.phtml

Listing 4.4. The header view script: view/layouts/_header.phtml

Listing 4.5. The Places_View_Helper_BaseUrl view helper

Listing 4.6. The footer view script: views/layouts/_footer.phtml

Listing 4.7. The Places_Controller_Plugin_ActionSetup front controller plug-in

Listing 4.8. The IndexController’s menuAction() function

Listing 4.9. The menu’s view script: application/views/scripts/index/menu.phtml

Listing 4.10. A partial menu item script

Listing 4.11. The IndexController’s advertAction() function

Listing 4.12. The advertAction() method’s view script: advert.phtml

Listing 4.13. The home page’s view script: index.phtml

Listing 4.14. A partial view script for the home page listing: _placeRow.phtml

Listing 4.15. The DisplayDate view helper

Listing 4.16. Setting up the expires meta tag using Zend_Date

Listing 4.17. Adding a website name with the postDispatch() method

Chapter 5. Ajax

Listing 5.1. A simple server-side validation routine in PHP: ajax.php

Listing 5.2. HTML file with simple form that requires validation: index.html

Listing 5.3. Hand-crafted JavaScript code to call back to the server for validation

Listing 5.4. Integrating YUI into our example Ajax application: ajax.js

Listing 5.5. IndexController action to display the page

Listing 5.6. IndexController::init() sets up the AjaxContext

Listing 5.7. YUI Ajax request to the controller action

Listing 5.8. The Place controller: application/controllers/PlaceController.php

Listing 5.9. Place view script providing location details and a list of reviews

Listing 5.10. __reviewItem.phtml displays one review

Listing 5.11. The HTML for review feedback within _reviewItem.phtml

Listing 5.12. The HTML for review feedback in _reviewFeedback.phtml

Listing 5.13. The ReviewFeedback class constructor

Listing 5.14. The JavaScript success callback method

Listing 5.15. ReviewController::init() sets up the AjaxContext

Listing 5.16. The server-side Ajax response

Listing 5.17. Echoing the view’s variables in feedback.json.phtml

Chapter 6. Managing the database

Listing 6.1. Creating a Zend_Db_Adapter instance with Zend_Db::factory()

Listing 6.2. Creating a Zend_Db_Adapter instance using Zend_Db::factory()

Listing 6.3. Inserting a row using Zend_Db_Adapter

Listing 6.4. Updating multiple rows using Zend_Db_Adapter

Listing 6.5. Using functions in SQL statements

Listing 6.6. Declaring a table class for use with Zend_Db_Table

Listing 6.7. Creating a model using Zend_Db_Table components

Listing 6.8. Overriding __get() to provide custom properties

Listing 6.9. The [test] section overrides [general] in application/config.ini Listing

Listing 6.10. Skeleton for unit test class: tests/models/UsersTests.php

Listing 6.11. Initializing the database in setUp()

Listing 6.12. Initializing the database in setUp()

Listing 6.13. One-to-many relationship using Zend_Db_Table

Listing 6.14. A many-to-many relationship using Zend_Db_Table

Listing 6.15. Reviews class with updated $_referenceMap

Chapter 7. User authentication and access control

Listing 7.1. Configuration of Zend_Auth_Adapter_Http

Listing 7.2. Authentication with Zend_Auth_Adapter_Http

Listing 7.3. The login form: auth/login.phtml

Listing 7.4. The identify action validates the username and password

Listing 7.5. _getAuthAdapter() sets up the authentication adapter

Listing 7.6. The LoggedInUser view helper

Listing 7.7. The auth/logout controller action

Listing 7.8. INI file entries to configure Zend_Acl roles

Listing 7.9. The extended Zend_Acl object

Listing 7.10. Setting up the ACL action helper

Listing 7.11. The ACL action helper’s rule methods

Listing 7.12. The ACL action helper’s preDispatch() method

Listing 7.13. More complex ACL rules so only admins can manage place records

Chapter 8. Forms

Listing 8.1. Using Zend_Filter filter chains to apply multiple filters to input data

Listing 8.2. Using validation chains to apply multiple input data checks

Listing 8.3. Our auth/form.phtml login view script using Zend_Form

Listing 8.4. Our refactored indexAction() controller action

Listing 8.5. Our formAction() in our AuthController controller action class

Listing 8.6. Our basic login form using Zend_Form

Listing 8.7. Adding filtering and validation to our login form elements

Listing 8.8. Adding custom messages to our login form elements

Listing 8.9. Our login form with translated error messages

Listing 8.10. Our username element with custom Authorise validator added

Listing 8.11. Our custom Authorise validator

Listing 8.12. Our login form HTML produced by the default decorators

Listing 8.13. Our login form with decorator settings

Listing 8.14. Our login form HTML produced by our custom decorator settings

Listing 8.15. The form.css CSS styles used to style our login form

Chapter 9. Searching

Listing 9.1. Adding a document to the search index

Listing 9.2. Extending Zend_Search_Lucene_Document for easier creation

Listing 9.3. Adding to the index

Listing 9.4. The Places_Db_Table_Row_Observable class

Listing 9.5. Notifying the observer after an insert

Listing 9.6. The notification hook method: SearchIndexer::observeTableRow()

Listing 9.7. Retrieving the field information: SearchIndexer::getDocument()

Listing 9.8. Adding to the search index: SearchIndexer::_addToIndex()

Listing 9.9. Deleting a document before adding it

Listing 9.10. Overriding open() so that it all works

Listing 9.11. Correcting SearchIndexer::_addToIndex()

Listing 9.12. The re-indexing controller

Listing 9.13. A simple search form in HTML

Listing 9.14. Filtering and validating for the search form

Listing 9.15. Displaying error messages from Zend_Filter_Input

Listing 9.16. Displaying error messages from Zend_Filter_Input

Listing 9.17. View helper to retrieve the search result’s URL

Chapter 10. Email

Listing 10.1. A simple Zend_Mail example

Listing 10.2. Setting up Zend_Mail to use an SMTP connection

Listing 10.3. One-to-many relationship using Zend_Db_Table

Listing 10.4. Our Support model class

Listing 10.5. The addAction() method in our SupportController action class

Listing 10.6. Overriding save() in our Support_Row subclass to trigger an email

Listing 10.7. Our Support_Mailer class, which will send out notification mail

Listing 10.8. Attaching the screenshot file to the support email

Listing 10.9. Plain text version of support-ticket submitter email in text-email.phtml

Listing 10.10. HTML version of support-ticket submitter email in html-email.phtml

Listing 10.11. Turning the support email into a row in the support table

Listing 10.12. Reducing a multipart email to its plain text component

Listing 10.13. Writing the message out to a file

Listing 10.14. Adding read mail functionality to our Support class

Chapter 11. Deployment

Listing 11.1. The contents of our index.php file

Listing 11.2. A stripped-down version of the Bootstrap class in bootstrap.php

Listing 11.3. The contents of our config.ini file

Listing 11.4. Our virtual host settings in Apache’s httpd.conf file

Listing 11.5. Checking out the latest work from the repository

Listing 11.6. Committing our changes to the repository

Listing 11.7. Updating our working copy from the repository

Listing 11.8. Using the export command to get a clean working copy

Listing 11.9. Our Selenium IDE test as a PHPUnit test case

Listing 11.10. Our Selenium IDE test rewritten to use Zend_Http_Client

Chapter 12. Talking with other applications

Listing 12.1. A feed-producing controller action

Listing 12.2. Rewrite rules modified to allow requests through to the XML-RPC server

Listing 12.3. The contents of our xmlrpc/index.php file

Listing 12.4. Using Zend_XmlRpc_Server in our bootstrap file

Listing 12.5. Our interface for the MetaWeblog API

Listing 12.6. Our MetaWeblog model implementing the interface from listing 12.5

Listing 12.7. Our Zend_XmlRpc_Server with caching implemented

Listing 12.8. Using Zend_XmlRpc_Client within a controller action

Listing 12.9. Adding error handling to our XML-RPC client

Listing 12.10. Sending a REST request to Akismet using Zend_Rest_Client

Listing 12.11. Sending a REST request to Akismet using Zend_Http_Client

Listing 12.12. Sending a REST request to Akismet using Zend_Rest

Listing 12.13. Our Places service application interface

Listing 12.14. Our Places service concrete class

Listing 12.15. Our REST server

Chapter 13. Mashups with public web services

Listing 13.1. Using Zend_Service_Akismet to filter a review for possible spam

Listing 13.2. Checking incoming links to Places!

Listing 13.3. Checking indexing of our site

Listing 13.4. The Amazon model class that will be used for data retrieval

Listing 13.5. Our AmazonAds view helper that composes the HTML for our view

Listing 13.6. Our AmazonAds view helper with added caching using Zend_Cache

Listing 13.7. Our Flickr model class

Listing 13.8. Using our Flickr model class in our article controller action

Listing 13.9. The article view with the added Flickr images

Listing 13.10. Our videos controller action

Listing 13.11. Our video categories view code

Listing 13.12. Our video list view file

Listing 13.13. Our video view file

Chapter 14. Caching: making it faster

Listing 14.1. Retrieving database data without caching

Listing 14.2. Retrieving database data with caching

Listing 14.3. Example usage of Zend_Cache

Listing 14.4. Simple use of Zend_Cache_Core

Listing 14.5. Using Zend_Cache_Core to store database results

Listing 14.6. Using cache code inside a model

Listing 14.7. Usage of Zend_Cache_Frontend_Output

Listing 14.8. Example function call without caching

Listing 14.9. Example function call with caching

Listing 14.10. Example usage of Zend_Cache_Frontend_File

Chapter 15. Internationalization and localization

Listing 15.1. Number normalization with Zend_Locale

Listing 15.2. Number localization with Zend_Locale

Listing 15.3. Date normalization with Zend_Locale

Listing 15.4. Standard PHP output

Listing 15.5. Translated version of Listing 15.4

Listing 15.6. Zend_Translate using the gettext() adapter

Listing 15.7. Setting locale information in config.ini

Listing 15.8. Implementing a new routing rule for language support

Listing 15.9. LanguageSetup front controller plug-in

Listing 15.10. The LanguageSetup’s dispatchLoopStartup() method

Listing 15.11. The top part of the non-localized index.phtml

Listing 15.12. The localized version of index.phtml

Listing 15.13. The English translation file, en_GB.php Listing

Listing 15.14. The German translation file, de_DE.php

Listing 15.15. Creating URLs using the url() view helper

Listing 15.16. Naïve localization of dates

Listing 15.17. Localization of dates using Zend_Date

Chapter 16. Creating PDFs

Listing 16.1. The report document model class forms the basis for our report generator

Listing 16.2. The initial setup code for our Report_Page model class

Listing 16.3. The setHeader() method of our Report_Page class

Listing 16.4. Our wrapText() method, which will allow us to draw wrapped text

Listing 16.5. Creating a style set for our Report_Page class

Listing 16.6. Drawing dashed lines in a notes section

Listing 16.7. The getGraphSection() method of our Report_Page class

Listing 16.8. The render() method of our Report_Page class

Listing 16.9. Using the report models in our ReportController controller action

Appendix A. A whistle-Stop Tour of PHP Syntax

Listing A.1. A Simple PHP Script, Listing1.php

Listing A.2. Simple variable expansion within strings

Listing A.3. Complex variable expansion within strings

Listing A.4. The if() syntax

Listing A.5. The switch() syntax

Listing A.6. Using break and continue within loops

Listing A.7. The for() syntax

Listing A.8. The foreach() syntax

Listing A.9. Alternative syntax for control structures

Listing A.10. An example of a function to display a name

Appendix B. Object-Oriented PHP

Listing B.1. The most basic implementation of a class

Listing B.2. Adding a constructor to our class from listing B.1

Listing B.3. A simple class

Listing B.4. Extending ZFiA_Person to create ZFiA_Author

Listing B.5. An abstract class contains undefined functions

Listing B.6. Creating a concrete class from an abstract one

Listing B.7. An interface, specifying methods that must be defined in the class

Listing B.8. Providing information about function parameters with type hinting

Listing B.9. Zend_Config’s use of __get() and __set()

Listing B.10. Iterating over a directory listing using DirectoryIterator

Listing B.11. Using iteration with Zend_Db_Table_Rowset

Listing B.12. Iterator implementation within Zend_Db_Table_Rowset_Abstract

Listing B.13. Array operations with an ArrayAccess-capable class

Listing B.14. The Singleton pattern as implemented by Zend_Controller_Front

Listing B.15. Storing an object to the Zend_Registry

Listing B.16. Retrieving an object from the Zend_Registry

Listing B.17. The Registry pattern as implemented by Zend_Registry

Appendix C. Tips and tricks

Listing C.1. Front controller setup for Places in chapter 3

Listing C.2. Adding modules to the front controller setup for Places from Chapter 3

Listing C.3. Using ModelDirSetup to set the include path to the module’s models

Listing C.4. Creating a route for news/{id number} in the bootstrap class

Listing C.5. Creating a route with defaults and requirements

Listing C.6. Creating a static route to /login

Listing C.7. News archive route using Zend_Controller_Router_Route_Regex

Listing C.8. Logging data with Zend_Log

Listing C.9. Enabling database profiling within config.ini

Listing C.10. Logging SQL query data within the fetchLatest() model function

Listing C.11. Logging all SQL profile data at the end of dispatching