.571 7.50 x 9.25 7.50 x 9.25 Guide to Programming with ZEND FRAMEWORK Cal Evans MSRP $32.99 USD From the publishers of Shelve under PHP/Web Development/Internet Programming php|architect’s Guide to Programming With Zend Framework Cal Evans php|architect’s Zend PHP 5 Certication Study Guide Zend's new PHP 5 Certication Exam represent an excellent tool for professional PHP developers who want to distinguish themselves in their eld. php|architect's Zend PHP 5 Certication Study Guide, edited and produced by the publishers of php|architect magazine, provides the most comprehensive and thorough preparation tool for developers who wish to take the exam. This book provides complete coverage of every topic that is part of the exam, including: ✔ PHP Basics ✔ Functions ✔ Arrays ✔ Strings and Patterns ✔ Web Programming ✔ Object Oriented Programming ✔ Database Programming ✔ Object-oriented Design ✔ XML and Web Services ✔ Security ✔ Streams and Network Programming ✔ Dierences Between PHP 4 and 5 NEW IN THE SECOND EDITION: advanced database topics (PDO/mysqli), errata, new examples, and much, much more! php|architect’s Licensed to: Wei Dai neriodavid@gmail.com User #39728 php|architect’s Guide to Programming with Zend Framework by Cal Evans Licensed to 39728 - Wei Dai (neriodavid@gmail.com) php|architect’s Guide to Programming with Zend Framework Contents Copyright ©2007-2008 Calvin Evans – All Rights Reserved Book and cover layout, design and text Copyright ©2004-2008 Marco Tabini & Associates, Inc. – All Rights Reserved First Edition: January 2008 ISBN: 978-0-9738621-5-7 Produced in Canada Printed in the United States No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by means without the prior written permission of the publisher, excet in the case of brief quotations embedded in critical reviews or articles. Disclaimer Although every effort has been made in the preparation of this book to ensure the accuracy of the information contained therein, this book is provided “as-is” and the publisher, the author(s), their dis- tributors and retailers, as well as all affiliated, related or subsidiary parties take no responsibility for any inaccuracy and any and all damages caused, either directly or indirectly, by the use of such informa- tion. We have endeavoured to properly provide trademark infor mation on all companies and products mentioned in the book by the appropriate use of capitals. However, we cannot guarantee the accuracy of such information. Marco Tabini & Associates, The MTA logo, php|architect, the php|architect logo, NanoBook and the NanoBook logo are trademarks or registered trademarks of Marco Tabini & Associates, Inc. Written by Cal Evans Published by Marco Tabini & Associates, Inc. 28 Bombay Ave. Toronto, ON M3H 1B7 Canada (416) 630-6202 / (877) 630-6202 info@phparch.com / www.phparch.com Publisher Marco Tabini Technical Reviewer Matthew Weier O’Phinney Layout and Design Arbi Arzoumani Managing Editor Elizabeth Naramore Finance and Resource Management Emanuela Corso Licensed to 39728 - Wei Dai (neriodavid@gmail.com) Licensed to 39728 - Wei Dai (neriodavid@gmail.com) Licensed to 39728 - Wei Dai (neriodavid@gmail.com) Dedications I would like to dedicate this book to the following people, without whom, it would not have happened: • To my mother—for instilling in my my love of writing. • To my wife, the lovely and talented Kathy—who I love dearly because she puts up with me. • To my kids, Becky and J.C.—who I love an adore, even when I’m ignoring them to write. • To Marco, Elizabeth, Paul and Sean—for friendship. • To Mark de Visser—for the greatest job I’ve ever had. • To Matthew Weier O’Phinney—for being nice when you could have been mean. • To Mr. Jimmy Buffet—You don’t know me but I could not have finished this book without your music. My Jimmy Buffet playlist is my bank of bad habits. Licensed to 39728 - Wei Dai (neriodavid@gmail.com) Licensed to 39728 - Wei Dai (neriodavid@gmail.com) Contents Foreword xiii Chapter 1 — What makes the Frame-work 1 Why Use a Framework? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Which Framework is Right for Me? . . . . . . . . . . . . . . . . . . . . . . 1 Why Zend Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 M VC in a Nutshell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Introducing Zend Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 A Brief History of Zend Framework . . . . . . . . . . . . . . . . . . . . . 4 The Zend Framework Community . . . . . . . . . . . . . . . . . . . . . . 5 The Zend Framework License and Intellectual Property Concerns . . . 5 What You Need To Go From Here . . . . . . . . . . . . . . . . . . . . . . . . . . 5 S ummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Chapter 2 — Getting Started 9 Building Your First App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Step 1: Download a copy of Zend Framework . . . . . . . . . . . . . . . 10 S tep 2: Create Your Directory Structure . . . . . . . . . . . . . . . . . . . 11 Step 3: Create Your Bootstrap File . . . . . . . . . . . . . . . . . . . . . . 12 Step 4: Create Your .htaccess File . . . . . . . . . . . . . . . . . . . . . . . 15 Step 5: Create Your Controller . . . . . . . . . . . . . . . . . . . . . . . . . 16 Step 6: Fire Up a Browser and Revel In Your Handiwork . . . . . . . . . 17 S ummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Licensed to 39728 - Wei Dai (neriodavid@gmail.com) viii ” CONTENTS Chapter 3 — The Controller 21 Laying the Groundwork for a Sample Application . . . . . . . . . . . . . . . . 21 Creating a Sample Application . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 BaseController . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Helpers and Plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 P lacing the Helper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Using the Helper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Chapter 4 — The Model 41 T ypes of Model Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . 41 No Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Light Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Heavy Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Adding Registration & Login . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 C reating and Connecting to the Database . . . . . . . . . . . . . . . . . . 42 The Member Class-Registering New Members . . . . . . . . . . . . . . . 44 Allowing Members to Login . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Chapter 5 — The View 59 I nstantiating the View and Rendering Output . . . . . . . . . . . . . . . . . . 59 View Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Escaping Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 View Helpers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Su mmary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Chapter 6 — Data Access 73 C onnecting to the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Fetching Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 fetchAll() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 fetchAssoc() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 fetchCol() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 f etchPairs() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 fetchRow() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Licensed to 39728 - Wei Dai (neriodavid@gmail.com) CONTENTS ” ix fetchOne() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Profiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Chapter 7 — Authentication 87 About Zend_Auth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 U sing Zend_Auth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Chapter 8 — Super Secret Ninja Class: Globals.php 99 Setting Up Globals.php . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 Using Globals.php with Zend_Cache . . . . . . . . . . . . . . . . . . . . . . . 103 S toring Global Configuration Values . . . . . . . . . . . . . . . . . . . . . . . . 113 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 Chapter 9 — Web Services 121 Introduction to Flickr’s API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Integrating Yahoo! and Flickr APIs . . . . . . . . . . . . . . . . . . . . . . . . . 123 Creating Your Own Web Service . . . . . . . . . . . . . . . . . . . . . . . . . . 128 S ummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 Chapter 10 — Exceptions 137 Exceptions: A Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 Ch apter 11 — Rich Internet Applications 155 Making our Sample App Into an RIA . . . . . . . . . . . . . . . . . . . . . . . . 156 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 Chapter 12 — Zend Framework Party Tricks 167 Cleaning Your Cache Through CLI . . . . . . . . . . . . . . . . . . . . . . . . . 167 Setting up the Bootstrap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 C reating a New Bootstrap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 3 ProcessController.php . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 Licensed to 39728 - Wei Dai (neriodavid@gmail.com) [...]... are going to build • Zend Framework has a supportive community to help you when you get stuck • You need to have a firm grasp of object oriented programming in PHP 5 before you try and start working with Zend Framework • Zend Framework is not a miracle tool, it is a power tool Like any tool, understanding when not to use it is as important as understanding when and how to use it Licensed to 39728 -... one framework likely to be well known and understood by a large number of developers With the support available from Zend, developers ought to be able to get up to speed easily The engineers who have contributed to this framework have a deep understanding of the nuances of PHP and how to avoid the performance traps inherent in building layers between PHP and your application In other words, Zend Framework. .. oversight but a conscious decision by the framework team A Brief History of Zend Framework Coding on Zend Framework officially started in July of 2005 It was announced to the general public in the same year at the first ZendCon as one part of Zend s PHP Collaboration Project (The other two parts of the initiative are Zend s Developer Zone and an Eclipse based IDE for PHP The first public release was on March... Object Oriented Programming in PHP 5 Zend Framework is all about Object Oriented Programming If you don’t understand PHP 5’s object model, I recommend you stop reading, visit the PHP manual and check out the Object Oriented section You can get a lot of what you need there (http:/ /php. net/manual/en/language.oop5 .php) but there are other sections you need to read as well You will have to know the difference... a framework, is to find one that allows you to work in a way that is natural to you, provides you the services that are most common to your application and allows you to concentrate on building the code business logic of your application To be fair though, the right framework for you is the one that lets you be productive quickest and leverage your new skills the longest Why Zend Framework The Zend Framework. .. Download a copy of Zend Framework Figure 2.1 Zend Framework is broken up into 2 major parts, the library and the incubator The library directory contains the officially released code that has been vetted and blessed The incubator is for code that has passed the Proposals procedure but is not yet mature enough to be included in the main library The library directory contains the main Zend Framework If you... code is covered by test cases using PHPUnit Zend Framework was built on several key concepts: • Best Practices • Community Driven • Extensionability • Extreme Simplicity • Liberal BSD License Unlike many other frameworks available for PHP Zend Framework chose not to im, plement the ActiveRecord pattern and not to ship with an Object-Relation Mapper (ORM) Contrary to popular opinion, this was not an... critic of PHP frameworks for several reasons, one of which is the plethora of frameworks available The chief advantage of using a framework is maintainability - frameworks provide a system and method for organizing code, but each framework is different, and if developers have to start from scratch with each new one then the advantage is lost Having an official framework in the form of the Zend Framework. .. the BSD License can be found on Zend Framework Web site (http:/ /framework .zend. com/license) What You Need To Go From Here Learning any framework is a daunting task In this book, my goal is to get you up and over the learning curve so you can be productive faster There are a few things Licensed to 39728 - Wei Dai (neriodavid@gmail.com) The Zend Framework Community you need to make things easier First,... Whether you prefer mailing lists, forums or chat, Zend Framework community is always there and willing to help The community realized early on that with any framework, getting started is the hardest part Therefore, there are numerous tutorials and quickstart guides to help both novice and advanced users get up and running The project Web site (http:/ /framework .zend. com) houses not only the documentation . 9.25 Guide to Programming with ZEND FRAMEWORK Cal Evans MSRP $32.99 USD From the publishers of Shelve under PHP/ Web Development/Internet Programming php| architect s Guide to Programming With Zend. #39728 php| architect s Guide to Programming with Zend Framework by Cal Evans Licensed to 39728 - Wei Dai (neriodavid@gmail.com) php| architect s Guide to Programming with Zend Framework Contents Copyright. entities, Zend Framework was released under a BSD style license. This allows for the framework to be used in the widest possible range of projects and puts the fewest restrictions on adopters. A