ebook img

Zend Framework, A Beginner's Guide PDF

465 Pages·2010·2.277 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Zend Framework, A Beginner's Guide

Zend Framework: A Beginner’s Guide Vikram Vaswani New York Chicago San Francisco Lisbon London Madrid Mexico City Milan New Delhi San Juan Seoul Singapore Sydney Toronto Copyright © 2010 by The McGraw-Hill Companies. All rights reserved. Except as permitted under the United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the publisher. ISBN: 978-0-07-163940-8 MHID: 0-07-163940-3 The material in this eBook also appears in the print version of this title: ISBN: 978-0-07-163939-2, MHID: 0-07-163939-X. All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occurrence of a trademarked name, we use names in an editorial fashion only, and to the benefi t of the trademark owner, with no intention of infringement of the trademark. Where such designations appear in this book, they have been printed with initial caps. McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales promotions, or for use in corporate training programs. To contact a representative please e-mail us at [email protected]. Information has been obtained by McGraw-Hill from sources believed to be reliable. However, because of the possibility of human or mechanical error by our sources, McGraw-Hill, or others, McGraw-Hill does not guarantee the accuracy, adequacy, or completeness of any information and is not responsible for any errors or omissions or the results obtained from the use of such information. TERMS OF USE This is a copyrighted work and The McGraw-Hill Companies, Inc. (“McGrawHill”) and its licensors reserve all rights in and to the work. Use of this work is subject to these terms. Except as permitted under the Copyright Act of 1976 and the right to store and retrieve one copy of the work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon, transmit, distribute, disseminate, sell, publish or sublicense the work or any part of it without McGraw-Hill’s prior consent. You may use the work for your own noncommercial and personal use; any other use of the work is strictly prohibited. Your right to use the work may be terminated if you fail to comply with these terms. THE WORK IS PROVIDED “AS IS.” McGRAW-HILL AND ITS LICENSORS MAKE NO GUARANTEES OR WARRANTIES AS TO THE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK, INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE, AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill and its licensors do not warrant or guarantee that the functions contained in the work will meet your requirements or that its operation will be uninterrupted or error free. Neither McGraw-Hill nor its licensors shall be liable to you or anyone else for any inaccuracy, error or omission, regardless of cause, in the work or for any damages resulting therefrom. McGraw-Hill has no responsibility for the content of any information accessed through the work. Under no circumstances shall McGraw-Hill and/or its licensors be liable for any indirect, incidental, special, punitive, consequential or similar damages that result from the use of or inability to use the work, even if any of them has been advised of the possibility of such damages. This limitation of liability shall apply to any claim or cause whatsoever whether such claim or cause arises in contract, tort or otherwise. For Tonka, who keeps asking "Why?", and Farah, who always knows the answer. About the Author Vikram Vaswani is the founder and CEO of Melonfire (http:// www.melonfire.com/), a consultancy firm with special expertise in open-source tools and technologies. He has 12 years of experience working with PHP and MySQL as a Web application developer and product manager, and has created and deployed a variety of PHP applications for corporate intranets, high-traffic Internet Web sites, and mission-critical thin-client applications. Vikram is also a passionate proponent of the open-source movement and is a regular contributor of articles and tutorials on PHP, MySQL, XML, and related tools to the community through his regular columns on the Zend Developer Zone and IBM DeveloperWorks. He is the author of Zend Technologies’ well-regarded PHP 101 series for PHP beginners, and his previous books include MySQL: The Complete Reference (http://www.mysql-tcr.com/),How to Do Everything with PHP & MySQL (http://www .everythingphpmysql.com/),PHP Programming Solutions (http://www.php-programming- solutions.com/), and PHP: A Beginner’s Guide (http://www.php-beginners-guide.com/). A Felix Scholar at the University of Oxford, England, Vikram combines his interest in Web application development with various other activities. When not dreaming up plans for world domination, he amuses himself by reading crime fiction, watching movies, playing squash, blogging, and keeping a wary eye out for Agent Smith. Read more about him and Zend Framework: A Beginner’s Guide at http://www.zf-beginners-guide.com. About the Technical Editor Ryan Mauger is the Lead Developer for Lupimedia (http://www.lupimedia.com/), a multimedia design agency in Somerset, England that specializes in bespoke content management systems for design-oriented Web sites. Ryan is a keen Zend Framework supporter and contributor, and can often be found answering questions and guiding people on IRC (#channel). When not evangelizing the Zend Framework, Ryan is a proud father, and enjoys escaping to the lakes for a spot of fly fishing. Read more about him and his work at http:// www.rmauger.co.uk/. Contents FOREWORD ......................................................................... xiii ACKNOWLEDGMENTS ........................................................... xiv INTRODUCTION .................................................................. xv 1 Introducing the Zend Framework ................................................................. 1 Overview ........................................................................................................................ 3 Features .......................................................................................................................... 4 Standards Compliance and Best Practices ............................................................ 4 Reusability ............................................................................................................ 4 Internationalization ............................................................................................... 5 Open Source .......................................................................................................... 5 Community Support .............................................................................................. 5 Unique Advantages ......................................................................................................... 5 Loose Coupling ..................................................................................................... 5 Rapid Release Cycle ............................................................................................. 6 Unit Testing Policy ............................................................................................... 6 Code-Generation Tools ......................................................................................... 6 Market Credibility ................................................................................................ 6 Third-Party Application Interoperability .............................................................. 6 Commercial Support Options ............................................................................... 7 Extensive Documentation ..................................................................................... 7 Application Environment ............................................................................................... 7 Installing the Zend Framework ...................................................................................... 8 Try This 1-1:Starting a New Project ............................................................................. 10 Understand Application Requirements ................................................................. 11 Create the Application Directory .......................................................................... 11 Create the Application Skeleton ........................................................................... 11 Add Zend Framework Libraries ........................................................................... 13 Define Virtual Host Settings ................................................................................. 13 Using the Command-Line Tool ...................................................................................... 14 Summary ........................................................................................................................ 17 2 Working with Models, Views, Controllers, and Routes ............................... 19 Understanding Basic Concepts ....................................................................................... 20 Models .................................................................................................................. 21 Views .................................................................................................................... 22 Controllers ............................................................................................................ 23 v vi Zend Framework: A Beginner’s Guide Modules ................................................................................................................ 24 Routes ................................................................................................................... 25 Layouts ................................................................................................................. 25 Understanding Component Interaction .......................................................................... 26 Looking Behind the Default Index Page .............................................................. 28 Understanding the Modular Directory Layout ............................................................... 30 Try This 2-1:Using a Modular Directory Layout .......................................................... 32 Creating the Default Module ................................................................................ 33 Updating the Application Configuration File ....................................................... 33 Understanding Master Layouts and Custom Routes ...................................................... 33 Updating the Application Index Page ................................................................... 35 Setting a Master Layout ........................................................................................ 36 Using a Custom Route .......................................................................................... 37 Try This 2-2:Serving Static Content .............................................................................. 39 Defining Custom Routes ....................................................................................... 39 Defining the Controller ........................................................................................ 40 Defining the View ................................................................................................. 41 Updating the Master Layout ................................................................................. 42 Summary ........................................................................................................................ 44 3 Working with Forms ....................................................................................... 47 Understanding Form Basics ........................................................................................... 48 Creating Forms and Form Elements ............................................................................... 53 Working with Form Elements ............................................................................... 55 Setting Required and Default Values .................................................................... 65 Filtering and Validating Form Input ............................................................................... 70 Using Input Filters ................................................................................................ 70 Using Input Validators .......................................................................................... 73 Retrieving and Processing Form Input ................................................................. 81 Try This 3-1:Creating a Contact Form .......................................................................... 82 Defining the Form ................................................................................................. 82 Using a Custom Namespace ................................................................................. 85 Defining a Custom Route ..................................................................................... 85 Defining Controllers and Views ........................................................................... 86 Updating the Master Layout ................................................................................. 88 Customizing Form Appearance ...................................................................................... 90 Using Custom Error Messages ............................................................................. 91 Using Display Groups ........................................................................................... 93 Using Decorators .................................................................................................. 94 Summary ........................................................................................................................ 99 4 Working with Models ...................................................................................... 101 Understanding Models ................................................................................................... 102 Model Patterns ...................................................................................................... 105 Model Scope ......................................................................................................... 105 Contents vii Installing Doctrine .......................................................................................................... 107 Try This 4-1:Generating and Integrating Doctrine Models ........................................... 108 Initializing the Application Database .................................................................... 109 Generating Doctrine Models ................................................................................. 113 Setting Model Relationships ................................................................................. 115 Autoloading Doctrine ........................................................................................... 116 Working with Doctrine Models ...................................................................................... 118 Retrieving Records ............................................................................................... 118 Adding, Updating, and Deleting Records ............................................................. 120 Try This 4-2:Retrieving Database Records ................................................................... 122 Creating a New Module ........................................................................................ 122 Defining a Custom Route ..................................................................................... 122 Defining the Controller ......................................................................................... 122 Defining the View ................................................................................................. 124 Summary ........................................................................................................................ 127 5 Handling CRUD Operations .......................................................................... 129 Try This 5-1:Creating Database Records ...................................................................... 130 Defining the Form ................................................................................................. 130 Defining Controllers and Views ........................................................................... 136 Working with Administrative Actions ............................................................................ 140 Structure ................................................................................................................ 140 Routing ................................................................................................................. 141 Layout ............................................................................................................................. 142 Try This 5-2: Listing, Deleting, and Updating Database Records ................................. 142 Setting the Administrative Layout ........................................................................ 142 Defining Custom Routes ....................................................................................... 144 Defining the List Action and View ....................................................................... 146 Defining the Delete Action ................................................................................... 148 Defining the Update Form .................................................................................... 150 Defining the Update Action and View .................................................................. 154 Updating the Display Action ................................................................................ 157 Adding User Authentication ........................................................................................... 158 Try This 5-3:Creating a Login/Logout System ............................................................. 161 Defining Custom Routes ....................................................................................... 161 Defining the Login Form ...................................................................................... 161 Defining the Authentication Adapter .................................................................... 162 Defining the Login Action and View .................................................................... 165 Defining the Logout Action .................................................................................. 167 Protecting Administrative Actions ........................................................................ 167 Updating the Master Layout ................................................................................. 168 Summary ........................................................................................................................ 170 viii Zend Framework: A Beginner’s Guide 6 Indexing, Searching, and Formatting Data ................................................... 173 Try This 6-1:Searching and Filtering Database Records ............................................... 174 Defining the Search Form ..................................................................................... 174 Defining the Controller and View ......................................................................... 177 Updating the Master Layout ................................................................................. 179 Adding Full-Text Search ................................................................................................ 181 Indexing Data ........................................................................................................ 182 Searching Data ...................................................................................................... 184 Try This 6-2:Creating a Full-Text Search Engine ......................................................... 185 Defining the Index Location ................................................................................. 185 Defining Custom Routes ....................................................................................... 186 Defining the Index Action and View .................................................................... 186 Updating the Summary View ................................................................................ 188 Updating the Search Form .................................................................................... 188 Updating the Search Action and View .................................................................. 189 Handling Multiple Output Types .................................................................................... 192 Try This 6-3:Expressing Search Results in XML .......................................................... 194 Enabling the XML Context ................................................................................... 194 Defining the XML View ....................................................................................... 194 Summary ........................................................................................................................ 196 7 Paging, Sorting, and Uploading Data ............................................................ 199 Try This 7-1:Paging and Sorting Database Records ..................................................... 200 Adding Page Numbers to Routes .......................................................................... 201 Updating the Index Controller and View .............................................................. 201 Adding Sort Criteria to Routes ............................................................................. 204 Updating the Controller and View ........................................................................ 204 Working with File Uploads ............................................................................................ 209 Try This 7-2:Enabling Image Uploads .......................................................................... 213 Defining the Upload Destination .......................................................................... 213 Updating the Form Definition .............................................................................. 213 Updating the Create Action .................................................................................. 215 Updating the Display Action and View ................................................................ 216 Updating the Delete Action .................................................................................. 218 Working with Configuration Data .................................................................................. 222 Reading Configuration Files ................................................................................. 222 Writing Configuration Files .................................................................................. 224 Try This 7-3:Configuring Application Settings ............................................................. 227 Defining the Configuration Form ......................................................................... 227 Defining the Configuration File ............................................................................ 229 Defining Custom Routes ....................................................................................... 230 Defining the Controller and View ......................................................................... 231 Updating the Master Layout ................................................................................. 233 Using Configuration Data ..................................................................................... 235 Summary ........................................................................................................................ 239 Contents ix 8 Logging and Debugging Exceptions .............................................................. 241 Understanding Exceptions .............................................................................................. 242 Understanding the Default Error-Handling Process ....................................................... 246 Using Custom Exception Classes ......................................................................... 249 Controlling Exception Visibility ........................................................................... 251 Try This 8-1:Creating a Custom Error Page .................................................................. 252 Logging Data .................................................................................................................. 254 Writing Log Messages .......................................................................................... 254 Adding Data to Log Messages .............................................................................. 259 Formatting Log Messages ..................................................................................... 259 Try This 8-2:Logging Application Exceptions .............................................................. 263 Defining the Log Location .................................................................................... 263 Defining the Database Log Writer ........................................................................ 264 Updating the Error Controller ............................................................................... 265 Summary ........................................................................................................................ 268 9 Understanding Application Localization ...................................................... 271 Understanding Localization and Locales ....................................................................... 272 Setting the Application Locale .............................................................................. 274 Localizing Numbers ....................................................................................................... 277 Localizing Dates and Times ........................................................................................... 279 Localizing Currencies ..................................................................................................... 282 Localizing Measurements ............................................................................................... 285 Localizing Strings .......................................................................................................... 287 Working with Adapters and Data Sources ............................................................ 289 Using the Application Locale ............................................................................... 291 Using the Translation View Helper ....................................................................... 293 Try This 9-1:Localizing the Example Application ........................................................ 294 Setting the Application Locale .............................................................................. 294 Localizing Numbers and Dates ............................................................................. 294 Defining String Localization Targets .................................................................... 298 Creating Translation Sources ................................................................................ 300 Registering the Translation Object ....................................................................... 302 Supporting Manual Locale Selection .................................................................... 306 Updating the Master Layout ................................................................................. 307 Summary ........................................................................................................................ 309 10 Working with News Feeds and Web Services ............................................... 311 Working with News Feeds ............................................................................................. 312 Understanding News Feed Formats ...................................................................... 313 Consuming News Feeds ....................................................................................... 313 Creating News Feeds ............................................................................................ 317 Accessing Web Services ................................................................................................. 319 Understanding Web Services ................................................................................ 319 Consuming Web Services ..................................................................................... 322

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.