HomePHP Building a Simple MVC-based Framework in PHP 5
Building a Simple MVC-based Framework in PHP 5
In this first part of a series, I develop the first module of a model-view-controller (MVC) driven framework, which happens to be a front controller. By combining this component and a basic .htaccess file, it’s possible to route all HTTP requests to the (still-undefined) dispatcher class.
As you may know, one of the most popular approaches that can be taken for speeding up the development of PHP applications without having to start coding them from scratch is using either a proprietary or a third-party framework. Yes, nowadays PHP frameworks are available in so many flavors and sizes that you've more than likely already picked one up that neatly suits your particular needs.
Although many of these frameworks don’t stick to the schema imposed by a particular design pattern, the vast majority of them have adopted the Model-View-Controller paradigm as a standard that allows users to separate the business and application logic from the visual presentation.
Popular software packages like the Zend Framework, Kohana and CodeIgniter are good examples of frameworks that rely on the functionality offered by the MVC design pattern to build powerful PHP programs. This fact speaks for itself about the usefulness of this pattern, particularly when developing well-structured web applications.
However, you, as a curious PHP developer, may want to make your own way and learn how to build a MVC-based framework all by yourself. If this is the case, then you should start reading this series of articles, since I’m going to explain in a step-by-step fashion how to develop a simple framework in PHP 5. It will follow the structure dictated by the MVC design pattern.
Before I start showing some functional code samples, a word of warning is in order here. First off, this guide won’t encourage you to reinvent the wheel. As I said before, you’re free to choose an existing framework and get the job done in a few steps. Instead, this series is aimed at introducing the key concepts that surround the development of a basic MVC-based framework, including the construction of different components, such as a front controller and an action controller, a router/dispatcher and a database abstraction class, and of course a couple of model and view classes.
In addition, I plan to add to the framework some basic caching and input sanitization capabilities; this will be accomplished in forthcoming articles of the series. And last but not least, to complement the material included in this series, you can download the full source files that comprise this example framework and give it a try: