In This Chapter
Leveraging existing tools and patterns
Picking out a library and framework for Twooshes
Understanding the MVC design pattern
When you sit down to start coding your Web application, you shouldn't start from scratch. Much like how you don't have to start coding from 0's and 1's, you don't have to start from a blank PHP file. You can build on top of the previous generation's breakthroughs by using free, high-quality libraries, frameworks, and design patterns. Leveraging these tools saves you time, increases your code quality, and lets you get straight to the core of your project.
Earlier in this book, I showed you how to interact with the Twitter API. You may have noticed it is pretty straightforward but the code is a little redundant. Every time you connect to Twitter using cURL, and parse data using DOMDocument, the code looks the same as the last time you called the API. This is a great opportunity to create libraries, functions, or classes to reduce the amount of redundant code, and make interacting with the Twitter API easy.
By centralizing your code and removing redundancies, you make it easier to read, maintain, and consequently decrease the potential for bugs. However, you don't need to build your own library from scratch. Other developers have already created open-source libraries, in numerous programming languages, that you can use for free. Thanks to their efforts, you can skip all this legwork, and get to the part of development you're interested in.
If building libraries is what you're interested in, then by all means create your own. Or you can contribute to a pre-existing library and improve it for yourself and the rest of the Twitter community.
You can view a fairly comprehensive list of open source Twitter libraries, maintained by Twitter, on the Twitter API Wiki at http://apiwiki.twitter.com/Libraries
.
For the Twooshes project, I use the Twitter library found in the PHP Zend Framework. It isn't the most comprehensive Twitter PHP library available, but it's built into the rest of the Zend Framework.
Web application frameworks are code projects that encapsulate common Web development tasks. They serve as a Web application's foundation, which the developer then builds upon. The framework's purposes are to
Decrease development time, by relieving the developer from coding common Web application tasks
Increase the quality of the project, by abstracting common tasks using tested code.
They prevent you from reinventing the wheel.
They usually encourage good design patterns.
Having good design patterns and leveraging a popular framework also makes it easier for other developers to work on your product, due to recognizable methods and patterns.
For us PHP developers, there are several PHP frameworks. These are popular:
Agavi: http://agavi.org
CakePHP: http://cakephp.org
CodeIgniter: http://codeigniter.com
Symfony: http://symfony-project.org
Zend Framework: http://framework.zend.com
I use the Zend Framework (ZF) for the Twooshes project in this book because it is one of the most popular PHP frameworks. Finding books, reference material, and support groups for ZF is fairly easy. ZF also has a corporate sponsor, Zend Technologies, which is co-founded by two PHP core developers, Andi Gutmans and Zeev Suraski. There have also been technical contributions from corporations like IBM, Google, Microsoft, and Adobe Systems. With heavyweights like this working on ZF, you know it has to meet a high level of quality.
Another big selling point for ZF is it has a Twitter API library built in. In addition, ZF supports and encourages the model-view-controller design pattern.
This chapter is about avoiding reinventing things: design patterns are no exception. Design patterns are conceptual ideas on how to structure software that has been observed, reused, and tested in numerous applications.
Model-view-control (MVC) is a popular software design pattern. I use it on the Twooshes project in this book. It is named after its three conceptual parts:
Model: This is where data comes from.
View: This is where the HTML is.
Controller: This is where the business logic is, and where the view gets its data.
The strength of the MVC design pattern comes from the isolation of its three parts. By separating the HTML, data access code, and business logic, you divide your problem into smaller, more manageable pieces. This makes your application easier to build, scale, test, and maintain.
The MVC design pattern also requires isolation of its parts. The model and view aren't on speaking terms, and they always use the controller to talk to one another. The basic workflow for the MVC pattern is illustrated in Figure 10-1.
Walking through the MVC workflow goes something like the following:
A user requests a Web page.
The controller notices the requested Web page requires data from the database, so the controller asks for the data from the model.
The model gathers the data and gives it to the controller.
The controller hands the data over to the view to integrate it into the HTML.
The controller gives the completed HTML to the Web user.
Here are a few tips to help you conform to the MVC pattern:
You should never have SQL code in your view.
Your view should never reference anything in the model.
The controller should be lightweight, with as little code as possible.
The model can be fat with code, but it should only deal in gathering and interfacing with data.
Now that you have a grasp on the libraries, frameworks, and design patterns, and you know which ones are used to build Twooshes, it's time to set up our server.
3.149.245.219