Introduction

Honest differences are often a healthy sign of progress.

Mahatma Gandhi

For a long time, PHP was disregarded as a language not serious enough for rich web applications. Everyone knew it was popular and perhaps good for small one-shot projects, but all the praise was reserved for the aristocratic elite of frameworks such as Spring, Ruby on Rails, or Django. Only recently has the situation changed, and it changed dramatically. In 2007, it became clear that PHP has not just one, but three major web application frameworks extending capabilities of this language: Symfony, CakePHP, and Zend Framework. The pace of development was fast and steady. Object-oriented source code written in PHP5 was elegant and maintainable. More and more new projects began using them, and their successful completion made the PHP frameworks even more popular.

Nowadays, the popularity of PHP web development frameworks surpasses all others (the evidence is inside this book), and they have become a leading force in the industry. The aim of this book is to gather as much knowledge about this dynamic force as possible and portray all the features these frameworks provide to our fellow programmers.

WHO SHOULD READ THIS BOOK?

If you are actually looking for a vampire novel, put this book back on the shelf. Immediately. If you are a hard-core Assembler programmer who needs no web interfaces at all, you might not be interested, either. However, if you are involved in some kind of web development, you will probably find this book useful. It is thick and heavy enough to cover a wide range of topics and provide various perspectives for all kinds of readers:

  • Professional PHP web application developers were the first people we thought of when we started writing this book, perhaps because we are PHP programmers, too. Frameworks offer multiple advanced features that can make our lives easier and more exciting. That's why we wanted to dig deeper and try out whole potentials of different frameworks and thoroughly compare them for your pleasure and convenience.

  • Experts in Ruby on Rails, Django, TurboGears, Struts, ASP.NET, or other non-PHP frameworks who want to take a closer look at PHP. Instead of buying separate books for each framework or choosing one more or less at random, they can benefit from comparing examples hands-on. They can experience the differences between the frameworks, which sometimes are really subtle, and perhaps switch to PHP one day.

  • Students and PHP beginners should not be afraid of the complexity of some more advanced topics. This book is a tutorial, but it is also much more! We have put a lot of effort into making it accessible. The first part of this book, "The Basics," covers everything to get the whole thing (or even three things) running. The second part, "Common Tasks," is more than adequate to serve the needs of most academic courses or a plan of individual education. The rest of the book will be very useful if you decide to continue your romance with any one of the frameworks.

  • Project managers, analysts or system administrators who often decide on which technology to choose or who need a deeper understanding of existing computer systems and applications. We have prepared a whole part (Part 4, "Comparison") that is focused on comparing the three frameworks and discussing their capabilities.

  • Advanced non-web programmers, such as C++ application engineers or database experts who want to explore the vast world of web development, will find that this book is also a good starting point for them. They might be delighted with the object-oriented approach of PHP5, the rapid building process made possible with the frameworks, and all the advanced features provided by them. Meanwhile, the comparative approach provides a broad view of web-specific problems, and the tutorial side of the book prevents being stuck simply with more trivial tasks.

COMPARATIVE APPROACH

There are many great tutorials and books on each of the frameworks covered in this book. What makes this book unique is the comparative approach we've adopted. We wanted to do more than just present three advanced technologies — we wanted to point out their advantages and disadvantages by comparing how each solves certain problems. This gives you a very practical tutorial-like experience and a solid base for more advanced discussion. It allows you to formulate your own views on PHP web frameworks and their suitability for your needs.

Flame wars are a hallmark of all discussions about web frameworks. Everyone has a favorite and tries to promote it against all others. The problem is that all web frameworks are used for the same purpose, but have different internal structures. Knowing one of them is generally enough to produce web applications, so there are few people interested in mastering multiple tools of this kind. This makes comparisons difficult. No wonder many discussions are based on stereotypes, personal opinions, and unverified data.

In this situation, many unanswered questions arise: Which framework is best suited for my particular purpose? Which one is the quickest to learn? Which one produces applications the fastest? Which one has the richest features? Which one will I like best? Is there one that surpasses all the others? We have asked these questions ourselves and found no reliable answers. However, because these questions are often asked by other developers, we decided to do our best to find the solution and then share it in this book. The results were often really surprising.

STRUCTURE OF THIS BOOK

The main principle of this book is to show how to do some tasks in each framework (in parallel wherever possible). To accomplish this, each example is repeated for each framework. Sometimes the solutions are really similar in order to make all subtle differences easily visible, but sometimes one framework provides a unique solution, in which case we are not afraid to use it. The book is divided into four parts that will gradually introduce you to the complexities of PHP frameworks. More experienced developers can freely skip the first part or read only the chapters they need.

Basics

  • Chapter 1: Introducing Symfony, CakePHP, and Zend Framework — One of the biggest hardships with most frameworks is how to get started. This chapter addresses that problem with a comprehensive tutorial starting with a general discussion of web application frameworks, their structure, and the underlying Model-View-Controller (MVC) pattern. We also briefly present all available frameworks and explain why we chose Symfony, CakePHP, and Zend Framework for detailed comparison.

  • Chapter 2: Getting Started — Next we move to installation and configuration. We provide instructions for Windows, Linux, and MacOS operating systems for every framework as well as the chosen database and web server. This is a stage in which many things can go wrong and discourage an inexperienced developer, so we are extra meticulous.

  • Chapter 3: Working with Databases — All frameworks are installed over a database engine, so Chapter 3 is dedicated to mitigating differences between relational databases and the world of object-oriented programming. Then you learn how to communicate with a database from the level of the frameworks, which encompasses constructing an object model with schema files and direct communication with databases through a command-line interface.

  • Chapter 4: My First Application in the Three Frameworks — Finally some programming. With all frameworks properly configured and running in your favorite environment, it is time you wrote your first application. The address book example presented in this chapter explains how to use tools to develop web applications quickly and efficiently.

Common Tasks

  • Chapter 5: Forms — This part of the book focuses on the standard elements used by every web developer in his everyday work. The first of these elements are user input forms. You'll start with a simple problem of validating fields and then move on to customizing forms for various application needs. Finally, we'll discuss protection against automated forms submission, namely Captcha.

  • Chapter 6: Mailing — Mailing is another common task required in nearly all web applications. We need it for user registration, sending announcements, and commercial advertising. In this chapter, several mailing engines will be presented and implemented: SwiftMailer, CakeMailer, ZendMailer, and PHPMailer.

  • Chapter 7: Searching — This chapter starts with in-depth theoretical descriptions of full-text searching, commonly used algorithms, and approaches. Then we move to practical solutions using the popular search engines Sphinx, Lucene, and Google Custom Search.

  • Chapter 8: Security — Security issues are always important for a professional web developer. After reading this chapter, you will know how to provide secure connections and defend against the two most dangerous kinds of attacks: server-side XSS injections and client-side cross-side request forgeries (CSRF). We discuss the various types of dangers and introduce security measures.

  • Chapter 9: Templates — The last thing covered in this part of the book is something everyone should know: how to make a web app visually appealing. In this chapter, we first show you how to create a simple image gallery and then we compare native template engines of the frameworks with add-ons such as the very popular Smarty engine.

Advanced Features

  • Chapter 10: AJAX — The first of more advanced topics discussed in this part is Asynchronous JavaScript and XML, or AJAX. It allows various features that are both useful and impressive. The first that we discuss is autocompletion of text fields with strings from a given database. The second example is dynamic popup windows for fun and profit, and the third is a simple chat room for multiple users.

  • Chapter 11: Making Plug-ins — Plug-ins provide advanced functionalities that you need. This chapter discusses creating your own plug-ins. For Symfony and CakePHP, you will write a PDF creation tool, but Zend Framework plug-ins work in a somewhat different manner, so they will be discussed with an appropriate example.

  • Chapter 12: Integrating Web Services — Web applications cannot live alone. They need integration with other web services and we discuss how to do it here. This chapter discusses the two most common standards, REST and SOAP, as well as providing examples of their use.

  • Chapter 13: Back end — Most web applications have a content management system (CMS). This chapter shows how to implement simple CMSs and how to use more advanced plug-ins. We also introduce the topic of content management frameworks.

  • Chapter 14: Internationalization — Internationalization doesn't end with the use of UTF8 character encoding. This chapter covers everything you need to know in order to make a website truly multilingual, including right-to-left languages, user input, collation for sorting algorithms, date formats, and other localization techniques.

  • Chapter 15: TestingQuality is the word that best describes the emphasis of this chapter. Testing is a very important part of web application development. This chapter introduces basic testing, including manual and automatic functional tests using the Selenium testing suite; and also black box, grey box, and unit tests.

  • Chapter 16: User Management — Web 2.0 applications revolve around users, who log-in, socialize, and create content. This chapter discusses efficient and secure ways to authenticate users and grant them access to certain features, starting with Role-Based Access Control (RBAC) and access control lists (ACLs) provided by the frameworks, and then moving on to Lightweight Directory Access Protocol (LDAP), an enterprise-grade solution.

Comparison

  • Chapter 17: Performance — This last part has fewer chapters than the previous parts, but it starts with an important one. We show here how to use JMeter to run your own customized performance and load tests. We also present two benchmarks made by us: throughput of a simple CRUD application and something even more important: comparison of lines of code written to create this application.

  • Chapter 18: Summary — The last chapter summarizes everything we have learned in this book. It lists all the pros and cons of each framework, both from a programmer's point of view and the quality of applications that can be developed with their help. And we'll tell you which PHP framework is the best one.

Appendices

We feel really sorry for less-popular frameworks because some of them are really delicious, and we had to focus on three mainstream ones only. However, we added basic info on CodeIgniter, Lithium, and Agavi with some code examples. They are young but very promising, and have good chances to gain great popularity.

There are also a list of interesting web resources for download and further reading, and a glossary of acronyms and technical terms used in the book.

SOURCE CODE

The source code presented in this book is designed to illustrate technologies described in the chapters in which it appears. Consistent with the idea that you should be able to freely read the code, not figure it out, the snippets are as simple and informative as possible. We didn't aim to print full listings of all files in the book.

However, we wouldn't leave you without full working applications. They can be downloaded from the Wrox website at www.wrox.com or from a dedicated website maintained by us at www.phpframeworks.org. The advantages of this approach are that we can put all needed files in one convenient downloadable packet. What is even more important is that you can adapt the examples to newer versions of the rapidly evolving frameworks.

To find the source code at the Wrox website, simply locate the book's title (use the Search box or one of the title lists) and click the Download Code link on the book details page to obtain all the source code for the book. Code that is included on the website is highlighted in this book by the following icon:

SOURCE CODE

You'll find the filename in a code note such as this:

Note

Code snippet filename

Note

Because many books have similar titles, you might find it easiest to search by ISBN; this book's ISBN is 978-0-470-88734-9.

Once you download the code, just decompress it with your favorite compression tool. Alternately, you can go to the main Wrox code download page at www.wrox.com/dynamic/books/download.aspx to see the code available for this book and all other Wrox books.

CONVENTIONS

Conventions used in this book are pretty intuitive and straightforward. In order to distinguish inline source code from normal text, we are using a monospace font. The same applies to filenames and directories. Names of variables are additionally italicized (unless they appear in code snippets or listings, where they are not italicized). Names of all methods and functions have parentheses at the end in order to make more visible that they are methods; however, their arguments are usually omitted and the parentheses are empty, as in this ExampleMethod(). URLs are monospace_and_underlined.

Snippets of code look like this:

$ zf create model AddressBook

Italic font is used in multiple contexts:

  • When introducing new terms and important words.

  • When joking and generally not being completely serious.

In the whole book, "Symfony" is always capitalized, like any other specific name, even when referring to 1.x versions, which were called "symfony." It not only appeals to our aesthetic sense but it is also much easier to find in dense text this way.

CONTACT US

We have worked hard to make this book approachable, informative, and bug-free. If you have any comments or suggestions, please let us know. Also, if you find an error, you would do us a favor by telling us about it. More general info about this book, the authors, and an up-to-date list of errata can be found on our website at www.phpframeworks.org.

Also, if you ever wish to buy us a drink for job well done or insult us for massive incompetence, feel free to write us at .

Contact info for individual authors for more intimate proposals:

The authors (from left): Bartosz Porębski, Karol Przystalski and Leszek Nowak.

Figure 1. The authors (from left): Bartosz Porębski, Karol Przystalski and Leszek Nowak.

ERRATA

We make every effort to ensure that there are no errors in the text or in the code. However, no one is perfect, and mistakes do occur. If you find an error in one of our books, like a spelling mistake or faulty piece of code, we would be very grateful for your feedback. By sending in errata, you might save another reader hours of frustration, and at the same time, you will be helping us provide even higher-quality information.

To find the errata page for this book, go to http://www.wrox.com and locate the title using the Search box or one of the title lists. Then, on the book details page, click the Book Errata link. On this page, you can view all errata that have been submitted for this book and posted by Wrox editors. A complete book list, including links to each book's errata, is also available at www.wrox.com/misc-pages/booklist.shtml.

If you don't spot "your" error on the Book Errata page, go to www.wrox.com/contact/techsupport.shtml and complete the form there to send us the error you have found. We'll check the information and, if appropriate, post a message to the book's errata page and fix the problem in subsequent editions of the book.

P2P.WROX.COM

For author and peer discussion, join the P2P forums at p2p.wrox.com. The forums are a web-based system for you to post messages relating to Wrox books and related technologies and interact with other readers and technology users. The forums offer a subscription feature to e-mail you topics of interest of your choosing when new posts are made to the forums. Wrox authors, editors, other industry experts, and your fellow readers are present on these forums.

At http://p2p.wrox.com, you will find a number of different forums that will help you, not only as you read this book, but also as you develop your own applications. To join the forums, just follow these steps:

  1. Go to p2p.wrox.com and click the Register link.

  2. Read the terms of use and click Agree.

  3. Complete the required information to join, as well as any optional information you wish to provide, and click Submit.

  4. You will receive an e-mail with information describing how to verify your account and complete the joining process.

Note

You can read messages in the forums without joining P2P, but in order to post your own messages, you must join.

Once you join, you can post new messages and respond to messages other users post. You can read messages at any time on the Web. If you would like to have new messages from a particular forum e-mailed to you, click the Subscribe to this Forum icon by the forum name in the forum listing.

For more information about how to use the Wrox P2P, be sure to read the P2P FAQs for answers to questions about how the forum software works, as well as many common questions specific to P2P and Wrox books. To read the FAQs, click the FAQ link on any P2P page.