Dear Reader, Zend - Internet Archive

Transcription

CYANMAGENTAYELLOWBLACKPANTONE 123 CBOOKS FOR PROFESSIONALS BY PROFESSIONALS THE EXPERT’S VOICE IN OPEN SOURCECompanioneBook AvailableDear Reader,I began writing Beginning Zend Framework with one clear goal in mind: to getPHP developers interested in Zend Framework. This book is not a beginner’sguide to PHP; it is a beginner’s guide to Zend Framework. My goal was to writea step-by-step guide that is fun to read, while providing enough insight intothe process and the power of Zend Framework.After you have read this book, you will be equipped to build small-to medium-scale applications using Zend Framework. I’ll show you how to do thefollowing:You’ll soon be writing your own Zend Framework applications and seeingthe benefits it can bring to your own projects.I hope you have as much fun reading it as I had writing it!Armando PadillaTHE APRESS ROADMAPCompanion eBookBeginning PHP and MySQL,Third EditionSee last page for detailson 10 eBook versionZend FrameworkThe Definitive Guide toZend EnterpriseBeginningZend FrameworkPro ZendFramework CMSPractical Web 2.0Applications with PHPPro PHPwww.apress.comISBN 978-1-4302-1825-854299US 42.99PadillaSOURCE CODE ONLINEZend Framework Install Zend Framework in any environment so you can deploy yourapplications in any setup. Apply the MVC pattern to write flexible and efficient applications. Use Zend Framework to connect to a database. Mix external media, such as photos from Flickr, videos from YouTube, andproducts from Amazon.com, with your own by using web services. Send e-mail to keep in touch with your users. Add a customized search engine to your site. Optimize your web application using Zend Framework’s built-incaching tools.BeginningBeginning Zend FrameworkBeginningZendFrameworkLearn to build professional web applicationswith Zend FrameworkArmando PadillaShelve inProgramming/PHPUser level:Beginner–Intermediate9 781430 218258this print for content only—size & color not accuratespine 0.96875" 424 page count

Download at Boykma.Com

Beginning Zend Framework Armando PadillaDownload at Boykma.Com

Beginning Zend FrameworkCopyright 2009 by Armando PadillaAll rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,electronic or mechanical, including photocopying, recording, or by any information storage or retrievalsystem, without the prior written permission of the copyright owner and the publisher.ISBN-13 (pbk): 978-1-4302-1825-8ISBN-13 (electronic): 978-1-4302-1826-5Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrenceof a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademarkowner, with no intention of infringement of the trademark.Lead Editors: Steve Anglin and Matthew MoodieTechnical Reviewer: Adam DeFieldsEditorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Tony Campbell, GaryCornell, Jonathan Gennick, Michelle Lowman, Matthew Moodie, Jeffrey Pepper, FrankPohlmann, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom WelshProject Managers: Beth Christmas and Debra KellyCopy Editor: Nancy Sixsmith, ConText Editorial ServicesAssociate Production Director: Kari Brooks-CoponyProduction Editor: Ellie FountainCompositor: Tricia BronkellaIndexer: BIM IndexingCover Designer: Kurt KramesManufacturing Director: Tom DebolskiDistributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street,6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mailorders-ny@springer-sbm.com, or visit http://www.springeronline.com.For information on translations, please e-mail info@apress.com, or visit http://www.apress.com.Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use.eBook versions and licenses are also available for most titles. For more information, reference our SpecialBulk Sales–eBook Licensing web page at http://www.apress.com/info/bulksales.The information in this book is distributed on an “as is” basis, without warranty. Although everyprecaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have anyliability to any person or entity with respect to any loss or damage caused or alleged to be caused directlyor indirectly by the information contained in this work.The source code for this book is available to readers at http://www.apress.com.iiDownload at Boykma.Com

To my parents, Guillermo Padilla and Francisca Osuna. To my friendSusannah Halweg. You have shaped the person that I am today. Thankyou.Download at Boykma.Comiii

Contents at a Glance Contents. v About the Author. xiii About the Technical Reviewer . xiv Acknowledgments. xv Introduction . xvi Chapter 1: Getting Started with Zend Framework . 1 Chapter 2: The Application. 35 Chapter 3:Writing Controllers Using Zend Controller. 53 Chapter 4: Views, Forms, Filters, and Validators. 85 Chapter 5: Database Communication, Manipulation, and Display. 155 Chapter 6: Sending and Receiving E-mail . 227 Chapter 7: Web Services and Feeds . 253 Chapter 8: Creating a Search Engine Using Zend Search Lucene . 317 Chapter 9: Caching with Zend Framework. 359 Index . 387ivDownload at Boykma.Com

Contents Contents. v About the Author. xiii About the Technical Reviewer . xiv Acknowledgments. xv Introduction . xvi Chapter 1: Getting Started with Zend Framework . 1The Tools .1Apache .2Installing Apache on Windows .2Installing a Mac Development Environment.8Post-Apache Installation .11MySQL.13Installing MySQL on Windows.13Configuring MySQL .16Installing MySQL GUI Tools.18Post-MySQL Installation .21Creating a Test Database.22PHP.23Installing PHP.23Getting PHP and Apache to Talk.23Installing Zend Framework .25Your First Application.27Zend Tool.27Installing Zend Tool.28Testing the Zend Tool Installation.29Creating Your Project.29Looking Under the Hood.30Download at Boykma.Comv

Summary .33 Chapter 2: The Application. 35Music Mash-Up Application .35Mashing Up the Pieces .36Accounts Module .36Artists Module .36Web Services Module.37Designing the System .37An Overall Look .37Designing the Accounts Module.38Signing Up .39Logging In and Out .39User Profile Page .40User Update Page.41Designing the Artists Module.42Add Artist to User’s List .42Removing an Artist from the List .43Update Artist List .44Artist Profile Page .45Designing the Database.46Accounts Table.46Artists Table.48Accounts Artists Table .48Creating Tables in MySQL.50Summary .51 Chapter 3: Writing Controllers Using Zend Controller. 53Model-View-Controller Pattern .53Why Use MVC? .53What Is the MVC Pattern?.54MVC Life Cycle .55Zend Controllers .56Controller Guidelines .59Naming the Controller.59Extending Zend Controller Action.60viDownload at Boykma.Com

Naming Actions.61Routing in Zend Framework .61Transforming URLs to Controllers/Actions .62Dissecting the URL.62Creating Custom URLs .63Passing Parameters with Route.66Request Object .68Zend Controller Error Handling.77Extending Error Handling .78Setting a Different Error Handler.78Using Error Variables in the View .79The Model.80Summary .82 Chapter 4: Views, Forms, Filters, and Validators. 85Working with Views .85A Simple Example View .86Why You Need Views .94Manipulating the Directory Structure .98Adding Logic and Control to Views . 101Variable Handling . 101Looping in the View . 105If-Else Statements . 107Escaping User Input . 110Creating Your Own escape() Function . 111Advanced Escape Functionality. 112Creating Forms Using Zend Form . 115Getting Started . 115Adding Elements to a Form. 118Formatting the Form . 121Processing the Form . 123Error Handling . 126Adding Validation and Filtering. 129Creating Form Element Objects. 133Download at Boykma.Comvii

Creating Textarea Fields . 134Creating Password Fields . 140Creating Hidden Text Fields. 140Creating Radio Buttons . 140Creating Check Boxes . 140Creating Select Menus and Multiselect Menus . 141File Uploading. 144Implementing CAPTCHA . 147Summary . 151 Chapter 5: Database Communication, Manipulation, and Display. 155Getting Started . 155Installing PDO . 156Connecting to a Database . 157Inserting Data. 160Using Plain Old SQL (POSQL) . 161Inserting Data Without SQL . 163Database Expressions . 165Escaping Values. 166Brief Background: SQL Injection . 166Escaping User Data. 166Escaping by Using quoteInto() . 168Last Inserted ID . 170LoudBite Sign-up Page . 171LoudBite Add Artist. 173Fetching and Displaying Records . 175Using fetchAll(). 176Using fetchOne() . 181LoudBite Login Page . 183LoudBite User Profile Page. 188Deleting Records . 191Updating Records . 193Transaction Control. 195Object-Oriented SELECT Statements. 198Querying Records from Tables Using from() . 199viiiDownload at Boykma.Com

Querying Specific Columns. 200Executing Object-Oriented Statements. 201Creating Column and Table Aliases. 202Narrowing Down the Search with a WHERE Clause . 204Querying Two or More Tables using JOIN . 208Limiting and Ordering the Result Set . 211Ordering Result Sets. 211Database Expressions . 213Paginating Records . 215Using the Zend Paginator. 215Adding Control to the Paginator . 218Paginating Database Records. 223Summary . 225 Chapter 6: Sending and Receiving E-mail . 227Setting Up Mail. 227What Is Zend Mail?. 227Sending Your First E-mail. 228Sending E-mail Using SMTP . 230Setting More than One Recipient: Cc: and Bcc: . 234Adding a Cc:. 234Adding More than One Cc . 235Adding a Bcc: .

Beginning Zend Framework Dear Reader, I began writing Beginning Zend Framework with one clear goal in mind: to get PHP developers interested in Zend Framework. This book is not a beginner's guide to PHP; it is a beginner's guide to Zend Framework. My goal was to write a step-by-step guide that is fun to read, while providing enough insight into