Sams Teach Yourself PHP, MySQL and Apache All in One (2012)
Part VI. Administration and Fine-Tuning
Chapter 33. Using Application Frameworks
In this short chapter, you learn, including the following:
• What an application framework is and how it can help you
• The basics of the model-view-controller pattern of software architecture
• Considering and installing some popular PHP application frameworks
This book has taught you the basics of creating dynamic functionality within a website, be it individual scripts used to increase functionality or display dynamic data or a series of scripts hooked together in some consistent fashion to produce a web-based application. When you are ready to move to larger projects, in which you are likely to want to use functionality that others most certainly have used in their projects but you have neither the time nor inclination to reinvent the wheel, an application framework will become your new best friend.
Understanding Application Frameworks
Getting to the heart of the matter, an application framework is nothing more than a set of libraries and templates that enable you to rapidly develop feature-rich dynamic sites and web applications without building every piece of the puzzle from scratch. If you recall, in the chapters in Part V, “Basic Projects,” I was always careful to note that the examples showed but one of many ways to create scripts to achieve the overall goal. The use of an application framework allows you to say “I understand there are many ways to create a login sequence (or shopping cart, or discussion forum, and so on), and instead of starting from scratch, I will implement the [application framework] way of doing things.”
Besides the obvious benefit of reusing a stable codebase for common functionality, using a framework also helps a developer adhere to a consistent software architectural pattern. In the case of PHP frameworks, that pattern is typically the model-view-controller (MVC) pattern, which you learn more about in the next section. Another aspect of adhering to a framework, and thus some sort of software architecture, is the ability to implement a stable and consistent three-tier architecture for your application. In a three-tier architecture, you have a physical tier to represent the client, and then an application and database under your control and management. In other words, a user’s web browser or mobile device (client) connects directly to your application—and typically the presentation layer of it, or what you see in the browser—which in turn reaches back into the database to retrieve data to be presented.
It’s important to understand that some content management systems—installable software packages in their own right, such as WordPress (http://www.wordpress.org), Drupal (http://www.drupal.org), and Joomla (http://www.joomla.org)—are not technically application frameworks, but it is possible to install and customize the applications in such a way as to create your own flavor of it while reusing their feature-rich codebase.
Using the MVC Pattern
One goal of any software architecture pattern is to provide consistent structures, elements, and properties and attributes among the constituent pieces so as to ensure the transparency of the inner workings of the application for those developing and maintaining it. Think about the situation where one person has been coding an application alone without adhering to any architectural pattern: What happens when a new person joins the team, or the original person leaves it? Without a fundamental and shared body of knowledge—such as the adherence to a general architectural pattern—enormous amounts of time will be spent getting the new person up to speed, and that doesn’t even begin to touch on the issues he will have in consistently auditing, testing, and maintaining the hodgepodge code.
The MVC software architecture pattern is ready-made for web-based applications, and in fact many applications (or even just dynamic websites) adhere to some version of this pattern without even trying too hard. Figure 33.1 illustrates the MVC concept in a basic way, with solid lines indicating direct associations between each of the three components and dashed lines indicating what are sometimes indirect associations between some of the parts.
Figure 33.1 Visualizing the MVC pattern.
In general, the three components can be defined like this:
• Model—Stores and separates data. It is not a database itself, but rather think about the parts of your scripts to date that reach back into the database and pull information out of it, before it is displayed to the user.
• View—The visual representation of data in the model. These are like the parts of your scripts to date that have done nothing but display data you have retrieved from the database.
• Controller—The way in which the user invokes an action in a system. Often referred to as the “brains” of the application, it determines how the model will be used to achieve a successful result of that action.
Here’s another way of thinking about the flow in an MVC application. Assume that a user in an online store is viewing an item page and wants to add that item to her shopping cart:
1. The user presses a submit button on the page, with the mouse.
2. The controller accepts the form action and sends information to the model.
3. To show the user that an action and change in their data has occurred, the view communicates with the model to get data back out to show the user.
4. The controller waits for more user actions, when the cycle will repeat.
Each of the PHP frameworks discussed in this chapter enable you to easily apply an MVC pattern to your software applications. Many other PHP frameworks do as well, and although you might not choose to adhere to the pattern, it is recommended that you do so to enable easier testing, development, deployment, and ongoing maintenance of your applications.
For even more examples and explanations of the MVC pattern, see Jeff Atwood’s clear and concise blog post “Understanding Model-View-Controller” at http://www.codinghorror.com/blog/2008/05/understanding-model-view-controller.html.
Installing and Using PHP Application Frameworks
At the time of this writing, developers worldwide use more than 30 PHP application frameworks. Selecting a few to discuss in general was no small feat, but the ones I’m calling out in this chapter have a (relatively) long history and an active developer community with considerable uptake. In fact, those are three features unrelated to the code itself that you should think about when evaluating a framework for your own purposes: Has it been around awhile and is it stable? Are people actively using it? Is the parent company or group of developers actively maintaining it?
Other considerations for selection include the following:
• Determining whether the framework is best suited for the type of application you are creating; some frameworks are great for e-commerce, some for content publication, and some for both.
• Determining whether the framework provides you with the opportunity to use a software architecture pattern, and if so, whether it is the one you want to use.
• Determining whether the framework requires additional PHP modules or server libraries. If it does, but you do not control your server and therefore cannot modify the libraries and modules installed, that framework cannot work for you.
The next few sections cover the three popular open source PHP application frameworks: Zend, CakePHP, and CodeIgniter. Again, these are not the only possible frameworks; in fact, Symfony (http://www.symfony.com/) and Yii (http://www.yiiframework.com/) could easily have been included in the “best of breed” list.
Wikipedia maintains a comprehensive list of PHP application frameworks at http://en.wikipedia.org/wiki/Comparison_of_web_application_frameworks#PHP.
Considering the Zend Framework
If you are developing enterprise-grade PHP applications, seriously consider starting with the Zend Framework (http://framework.zend.com/). You might have heard of Zend, the company behind the Zend Framework, as its founders have been contributors to the PHP language itself almost since its initial creation. The core PHP engine is often referred to as the Zend engine. In other words, if evaluating the Zend Framework according to the criteria I previously mentioned, you would be hard-pressed to find a framework that is more stable, has been around longer, or has more people actively developing the framework and applications with it.
Although the name of the software is the Zend Framework, and indeed it is a framework, you might also hear it referred to as a component library because it is possible to pick and choose from a set of loosely coupled components instead of implementing a structured, well-architected application. For example, Zend’s separate components address database connections and profiling, internationalization and localization, authentication, authorization, and session management, consuming and exposing web services, and mail functions, to name a few.
That the framework is made of components is not a negative evaluation at all, but it does warrant a reminder that one of the goals of using frameworks and adhering to an architecture pattern is for the ease of maintenance—your application can be kept tidy, for lack of a better word. If your application includes a library of framework components and you are only using one of them, you might ask yourself if the overhead of maintaining the framework (updates, patches, and so on) is worth it. Your answers will vary, and only you will know which decision is right for you.
To download and install the Zend Framework with the existing PHP, Apache, and MySQL installation that you’ve been working with in this book, first go to the Requirements page at http://framework.zend.com/manual/en/requirements.introduction.html to ensure you have the correct version of PHP and any ancillary libraries and modules. If you do, go to http://framework.zend.com/download/latest for links to the Full or Minimal version of the software. After you have obtained the distribution archive in the format of your choice, just extract it according to the instructions included in the distribution. (In brief, you extract a directory and put it in your existing filesystem.) The Zend Framework QuickStart Guide, at http://framework.zend.com/manual/en/learning.quickstart.intro.html, walks you through the process of using the framework and its components through the creation of sample applications.
Another of the long-standing and stable PHP application frameworks with a strong user and developer community is CakePHP (http://www.cakephp.org/). At its core, CakePHP is an MVC framework, with components for common functionality such as database connections, authentication, authorization, and session management, and consuming and exposing web services—all much like the Zend Framework and, in fact, numerous other frameworks.
One of CakePHP’s greatest selling points is its ease of use (or ease of integration) while not allowing its simplicity to hinder feature-rich development and enterprise deployment. Like the Zend Framework, you do not have to deal with custom configuration files, and instead you just download and install the framework and get started with rapid development. From the perspective of the new user, CakePHP wins points for having detailed and user-friendly documentation as well as multimedia tutorials, in addition to the standard developer forums.
To download and install CakePHP with the existing PHP, Apache, and MySQL installation that you’ve been working with in this book, first go to the Installation page of the CakePHP Cookbook (also known as the documentation) at http://book.cakephp.org/2.0/en/installation.html to ensure you have the correct version of PHP and any ancillary libraries and modules. If you do, download the current version from links further down the page or on the main page of the site.
After you have obtained the distribution archive, just extract it into your web server document root. You have a few different options for using CakePHP in a development or production environment, all discussed in the Cookbook at http://book.cakephp.org/2.0/en/installation.html. Once CakePHP is installed, head to the QuickStart Guide at http://book.cakephp.org/2.0/en/getting-started.html for a walkthrough on using CakePHP to create some sample applications.
Although CakePHP is billed as easy to use (and it is), CodeIgniter (http://www.codeigniter.com) is a good starting place for true beginners. While feature rich, its functionality is targeted at delivering content rather than commerce or other transactional processes. This is not at all a bad thing, because delivering content in an easy-to-use manner within architecturally sound software is always a plus. I might not use CodeIgniter in the enterprise, but many web developers make a fine living with numerous happy clients without ever entering the enterprise space. So again, pick the framework that is right for you.
In addition to being an MVC framework, CodeIgniter addresses common functionality such as database connections, authentication, authorization, and session management, localization, image manipulation, and consuming and exposing web services, among others—much like the other frameworks discussed in this chapter. Again, like the other frameworks discussed, installation consists of downloading and installing the framework and beginning to develop immediately (and rapidly). Like CakePHP, CodeIgniter wins points for having detailed and user-friendly documentation as well as multimedia tutorials, in addition to the standard developer forums.
To download and install CodeIgniter with the existing PHP, Apache, and MySQL installation that you’ve been working with in this book, first go to the Server Requirements page of the documentation at http://codeigniter.com/user_guide/general/requirements.html to ensure you have the correct version of PHP and any ancillary libraries and modules. If you do, download the current version from the Downloads page at http://codeigniter.com/downloads/.
This short chapter introduced you to the concept of using an application framework, and specifically one using the model-view-controller pattern of software architecture. Before reading about three popular PHP application frameworks (Zend, CakePHP, and CodeIgniter), you learned the basics of how to evaluate a framework for use in your own application development.
The workshop is designed to help you review what you’ve learned and begin putting your knowledge into practice.
1. What are some of the benefits of using an application framework?
2. In the MVC pattern, what does the model do?
3. Do you have to use an application framework?
1. Working with a stable codebase, adhering to a software architecture pattern, and not reinventing the wheel.
2. The model stores and separates data from the controlling and viewing components.
3. Not at all. In fact, nothing in this book—which covers fundamental aspects of developing with PHP and MySQL—relies on a framework.
1. Download and install at least one of the frameworks discussed in this chapter. (If you install more than one, delete the old ones first to avoid collisions.)
2. Follow at least one of the tutorials provided by the developers of these frameworks so that you can gain some practical knowledge of using a framework as well as the MVC pattern.