Even at this likely early stage of your Web development career, chances are you're already attempting to sketch out the features of a long-desired custom application. An e-commerce store perhaps? An online community forum devoted to stamp collecting? Or maybe something a tad less interesting but nonetheless very practical, such as a corporate intranet? Regardless of the purpose, you should always strive to base development around sound development practices, several of which have become increasingly well-defined over time. In fact, focus on this area has become such that several groups of developers have banded together to produce a variety of Web frameworks, each of which serves to help others develop Web applications in a manner thats efficient, rapid, and representative of sound development principles. This chapter's purpose is threefold. First, the case is made for why you should seek to embrace one of the most crucial of these best practices, known as the Model-View-Controller (MVC) design architecture. Second, several of the most popular PHP-driven frameworks are introduced, each of which allows you to take advantage of MVC, in addition to a variety of other time-saving features such as Ajax integration. We devote additional time to the Zend Framework, which although the newest of the bunch, is rapidly becoming the most popular of these framework solutions. Introducing MVCThe advantages of the MVC architecture are perhaps best illustrated by providing an example of the problems that are sure to arise when it isn't implemented. Suppose you've recently launched a new Web site, only to find it's soon inundated with users. Eager to extend this newfound success, the project begins to grow in ambition and, as a result, in complexity. You've even begun to hire a few talented staff members to help out with the design and development. Well aware of your pathetic design skills, you're particularly keen for the designers to immediately begin an overhaul that will lead to a relaunch next month. Accordingly, you ask them to begin redesigning all of the sites pages, many of which look like this: <?php // Scrub some data // Retrieve desired employee's contact information // Parse and execute the query // Convert result row into variables ?> <div id="sectionheader">Recent Absences</div> // Retrieve employee absences in order according to descending date // Parse and execute the query // Output retrieved absence information // Include page footer Because the design and logic are inextricably intertwined, several problems soon arise:
You're probably noticing a pattern here: the lack of separation of concerns is breeding an environment of pain, distrust, and inefficiency. But there is a solution that can go a long way toward alleviating these issues: the MVC architecture. The MVC approach renders development more efficient by breaking the application into three distinct components: the model, the view, and the controller. Doing so allows for each component to be created and maintained in isolation, thereby minimizing the residual effects otherwise incurred should the components be intertwined in a manner similar to that illustrated in the previous example. While rather detailed definitions of each component exist in other learning resources, for the purposes of this introduction the following will suffice:
To help you better understand the dynamics of an MVC-driven framework, let's work through a typical scenario involving the converter application, highlighting the role of each MVC component:
The next section introduces four PHP-driven frameworks, each offering its own similar but unique MVC implementations.
blog comments powered by Disqus |