Contents


preface xvii
acknowledgments xix
about this book xxi
about the cover illustration xxvi

Part 1 The essentials 1

Introducing Zend Framework 3
Introducing structure to PHP websites 4
Why use Zend Framework? 5
Everything is in the box 6, Modern design 6, Easy to learn 6, Full documentation 7, Simple development 8, Rapid development 8, Structured code is easy to maintain 8
What is Zend Framework? 9
Where did it come from? 9, What’s in it? 9
Zend Framework design philosophy 15
High-quality components 15, Pragmatism and Flexibility 15, Clean IP 16, Support from Zend Technologies 16
Alternative PHP frameworks 16
Summary 17
Hello Zend Framework! 18
The Model-View-Controller design pattern 19
The model 20, The view 20, The controller 20
The anatomy of a Zend Framework application 21
The application directory 21, The library directory 21, The tests directory 21, The public directory 22
Hello World: file by file 22
Bootstrapping 22, Apache .htaccess 24, Index controller 25, View template 26
How MVC applies to Zend Framework 27
Zend Framework’s controller 28, Understanding Zend_View 32, The model in MVC 35
Summary 39

Part 3 A core application 41

Building a website with Zend Framework 43
Initial planning of a website 44
The site’s goals 44, Designing the user interface 45, Planning the code 47
Initial coding 48
The directory structure 48, The Bootstrap class 48, Running the application 53
The home page 53
The initial models 54, Testing our models 56, The home-page controller 59
Summary 63
Managing the view 64
Introducing the Two Step View and Composite View patterns 65
View handling with Zend_Layout 66
Integrating Zend_Layout into Places 67
Setup 67, Layout scripts 69, Common actions using placeholders 72, The homepage view script 76
Advanced view helpers 78
Controller integration 78, View script management 80, HTML header helpers 81
Summary 86
Ajax 87
Introducing Ajax 88
Defining Ajax 88, Using Ajax in web applications 89
A simple Ajax example 91
Using Ajax client libraries 94
Using Ajax with Zend Framework 95
The controller 96, The view 97
Integrating into a Zend Framework application 98
The Place controller 99, Adding review rating HTML to the view script 101, Adding JavaScript to the view scripts 102, The server code 104
Summary 106
Managing the database 107
Database abstraction with Zend_Db_Adapter 108
Creating a Zend_Db_Adapter 108, Querying the database 109 Inserting, updating, and deleting 110, Handling database-specific differences 111
Table abstraction with Zend_Db_Table 112
What is the Table Data Gateway pattern? 112, Using Zend_Db_Table 113, Inserting and updating with Zend_Db_Table 114, Deleting records with Zend_Db_Table 116
Using Zend_Db_Table as a model 116
Testing the model 118, Table relationships with Zend_Db_Table 123
Summary 127
User authentication and access control 128
Introducing authentication and access control 129
What is authentication? 129, What is access control? 129
Implementing authentication 130
Introducing Zend_Auth 130, Logging in using HTTP authentication 131
Using Zend_Auth in a real application 133
Logging in 133, A view helper welcome message 137, Logging out 138
Implementing access control 139
Introducing Zend_Acl 139, Configuring a Zend_Acl object 141, Checking the Zend_Acl object 142
Summary 146
Forms 147
Introducing Zend_Form 148
Integrated data filters and validators 148, Integrated error handling 150, Decorators to simplify markup 151, Plug-in loaders for customization 152, Internationalization 152, Subforms and display groups 152
Building a login form 153
Setting up paths 153, Our form view script 153, Updating the AuthController controller action 15, The basic login form class 155
Filtering and validation 156
Basic filtering and validation 156, Custom error messages 158, Internationalizing the form 159, Adding a custom validator 160
Decorating our login form 162
Zend_Form default decorators 162, Setting our own decorators 162
Summary 166
Searching 167
Benefits of search 168
Key usability issue for users 168, Ranking results is important 168
Introducing Zend_Search_Lucene 168
Creating a separate search index for your website 169, Powerful queries 171, Best practices 175
Adding search to Places 176
Updating the index as new content is added 176, Creating the search form and displaying the results 185
Summary 188
Email 189
The basics of email 190
Email simplified 190, Dissecting an email address 191
Introducing Zend_Mail 191
Creating emails with Zend_Mail 191, Sending emails with Zend_Mail 193
Building a support tracker for Places 195
Designing the application 195, Integrating Zend_Mail into the application 199, Adding headers to the support email 200, Adding attachments to the support email 202, Formatting the email 203
Reading email 205
Collecting and storing email 205, Reading email with our application 206
Summary 210
Deployment 211
Setting up the server 211
Designing for different environments 212, Using virtual hosts for development 214
Version control with Subversion 216
Creating the Subversion repository 217, Checking out code from the repository 218, Committing changes to the repository 218, Updating a local working copy 219, Dealing with conflicts 220, Getting a clean copy from the repository 222, Using branches 222, Externals 223
Functional testing 223
Functional testing with Selenium IDE 224, Automating Selenium IDE tests 226, Functional testing with Zend_Http_Client 227
Scripting the deployment 229
Summary 229

Part 3 More power to your application 231

Talking with other applications 233
Integrating applications 234
Exchanging structured data 234, Producing and consuming structured data 235, How web services work 236, Why we need web services 237
Producing and consuming feeds with Zend_Feed 237
Producing a feed 237, Consuming a feed 239
Making RPCs with Zend_XmlRpc 240
Using Zend_XmlRpc_Server 241, Using Zend_XmlRpc_Client 248
Using REST web services with Zend_Rest 250
What is REST? 250, Using Zend_Rest_Client 251, Using Zend_Rest_Server 254
Summary 256
Mashups with public web services 257
Accessing public web services 258
Zend_Gdata 258, Zend_Service_Akismet 260, Zend_Service_Amazon 260, Zend_Service_Audioscrobbler 261, Zend_Service_Delicious 261, Zend_Service_Flickr 261, Zend_Service_Gravatar 262, Zend_Service_Nirvanix 262, Zend_Service_RememberTheMilk 262, Zend_Service_Simpy 262, Zend_Service_SlideShare 263, Zend_Service_StrikeIron 263, Zend_Service_Technorati 263, Zend_Service_Yahoo 264
Displaying ads with Amazon web services 264
The Amazon model class 265, The Amazon ads view helper 266, Caching the view helper 267
Displaying pictures from Flickr 270
The Flickr model class 270, Using Flickr in an action controller 271
Using Zend_Gdata for Google access 274
The YouTube API in an action controller 274, The video categories page 275, The video list page 276, The video page 278
Summary 279
Caching: making it faster 280
Benefits of caching 281
How caching works 281
Implementing Zend_Cache 284
Zend_Cache frontends 284, Zend_Cache backends 295
Caching at different application levels 296
Choosing what to cache 296, Optimal cache expiry 297
Cache tags 298
Summary 298
Internationalization and localization 299
Translating languages and idioms 300
Translating languages 300, Translating idioms 301
Using Zend_Locale and Zend_Translate 301
Setting the locale with Zend_Locale 301, Translating with Zend_Translate 303
Adding a second language to the Places application 305
Selecting the language 305, The LanguageSetup front controller plug-in 308, Translating the view 310, Displaying the correct date with Zend_Locale 312
Summary 314
Creating PDFs 316
Zend_Pdf basics 317
Creating or loading documents 317, Creating pages in your PDF document 317, Adding document meta-information 318, Saving the PDF document 319
Building a PDF report generator 320
Our report document model 320, Our report page model 321
Drawing text on the page 322
Choosing fonts 322, Setting the font and adding text 323, Adding wrapped text 324
Working with color 324
Choosing colors 325, Setting colors 325
Using Styles 326
Drawing shapes 326
Drawing lines 327, Setting up dashed lines 327, Drawing rectangles and polygons 328, Drawing circles and ellipses 331
Rotating objects 332
Adding images to the page 332
Drawing objects within clipping masks 333
Generating PDF reports 333
Summary 335

 
appendix A A whistle-stop tour of PHP syntax 337
appendix B Object-Oriented PHP 350
appendix C Tips and tricks 367
index 381