Php|Architect's Guide to Programming with Zend Framework
Total Page:16
File Type:pdf, Size:1020Kb
php|architect’s php|architect’s Zend PHP 5 Certication Study Guide Zend's new PHP 5 Certication Exam represent an excellent tool for professional Guide to 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 Programming with 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: ZEND FRAMEWORK ✔ 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 Guide to Programming With Zend Framework Zend With Programming Guide to php|architect’s MSRP $32.99 USD From the publishers of Cal Evans Cal Cal Evans Shelve under PHP/Web Development/Internet Programming 7.50 x 9.25 .571 7.50 x 9.25 Licensed to: Wei Dai [email protected] User #39728 php|architect’s Guide to Programming with Zend Framework by Cal Evans 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 information 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 [email protected] / 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 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. 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 MVC 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 Summary ....................................... 7 Chapter 2 — Getting Started 9 Building Your First App ................................ 9 Step 1: Download a copy of Zend Framework . 10 Step 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 Summary ........................................ 17 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 Placing the Helper ............................... 36 Using the Helper ................................ 37 Summary ........................................ 38 Chapter 4 — The Model 41 Types of Model Implementation .......................... 41 No Model ..................................... 41 Light Model ................................... 42 Heavy Model ................................... 42 Adding Registration & Login ............................. 42 Creating 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 Instantiating the View and Rendering Output . 59 View Script ....................................... 60 Escaping Output ................................... 61 View Helpers ...................................... 61 Summary ........................................ 69 Chapter 6 — Data Access 73 Connecting to the Database ............................. 73 Fetching Data ..................................... 76 fetchAll() ..................................... 77 fetchAssoc() ................................... 79 fetchCol() ..................................... 79 fetchPairs() .................................... 80 fetchRow() .................................... 80 CONTENTS ” ix fetchOne() .................................... 81 Profiler ......................................... 81 Summary ........................................ 84 Chapter 7 — Authentication 87 About Zend_Auth ................................... 88 Using 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 Storing 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 Summary ........................................ 134 Chapter 10 — Exceptions 137 Exceptions: A Primer ................................. 137 Summary ........................................ 151 Chapter 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 Creating a New Bootstrap .............................. 173 ProcessController.php ................................ 176 Summary ........................................ 179 x ” CONTENTS Appendix A — Appendix A - Zend_Layout and doing the Two-Step 181 Index 199 Foreword I was delighted to be asked to provide a foreword for Cal’s book. He’s often quoted me as saying “All frameworks suck”. Given that context, you might be surprised that I believe this book should be a good read. I have long been a 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 means there is one framework likely to be well known and understood by a large number of devel- opers. 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 in- herent in building layers between PHP and your application. In other words, Zend Framework provides a great toolset for developers. As well as the MVC classes, the Zend Framework provides a set of really useful utility classes that can be integrated into any PHP application, regardless of how that application is architected. As such, there’s something to keep everyone happy. Cal has been preaching the Zend Framework at PHP conferences and in the De- vZone for some