A modern approach to MVC in php

With major articles on the use of the Model View Controller pattern in PHP being three to four years old, it's time to take a fresh look upon the subject. What has changed over the last years? What have we learned from other languages? How can we go beyond the MVC pattern to be agile, search engine friendly and safe without spending too much time writing complicated code? In this series, we will find answers to all those questions and more.

This introductory article is the beginning of a must-read guide for all people who want to write agile web applications using the MVC pattern in php.

Purpose

Ever since Joe Stump published his Understanding MVC in PHP back in 2005, a lot procedural PHP hackers started experimenting with object oriented programming. Because of this radical change, a lot of applications have been developed using bad practices and inefficient approaches towards object oriented design.

Luckily, a lot of PHP developers improved their understanding of solid object oriented programming while, at the same time, a massive amount of java developers switched to PHP to develop applications for the web. These developers soon realized that they were repeating themselves most of the time, writing code they had already written in another project. Developers all over the world started developing frameworks that allowed them to re-use a lot of their code. With these frameworks came good practices and conventions so that multiple developers on the same projects would write consistent and high-quality code.

Having tested a lot of these different frameworks, which by now have a really stable code-base, I soon realized that none of them would fully suit my needs. Even though application development was perfectly possible in each and every one of them, it was the conventions and choices made by other developers that often annoyed me. Considering the large amount of small, custom made frameworks, I have the feeling that a lot of developers suffer from these annoyances. Needless to say, these home-made frameworks are often plagued by bad design or general inconsistencies in the code.

This series is for developers eager to write their own framework using modern approaches, well designed code and consistent conventions. On top of that, this series will help developers alike to understand the code and practices behind some of the major MVC frameworks in PHP.

An introduction to MVC

To understand the often complicated structure of most major frameworks (and to be able to write our own), we will have to understand the basic pattern that is used as the foundation of these frameworks: the model-view-controller pattern. For now, these descriptions might be a bit brief, but we will give each of the parts an in-depth look in future articles. These descriptions are intended to define the full scope of the pattern and to grasp the general terminology used in the subsequent articles.

The controller

The controller is the core of the framework. It handles all the page-requests and makes sure that the other parts of the framework send and receive the correct information.

The model

Models handle the business-logic and the data-processing. They receive instructions from the controller and act upon it. They can send information back to the controller, to other models or directly to the view-layer.

The view

The view layer takes care of the presentation of data and information towards the end-user. It receives this information from the controller or the models and, in turn, sends requests to the controller based on user interaction.

What's next

In the upcoming article, we will start out by setting up our environment, defining a basic folder structure and handling a basic page-request through our mini MVC-system.

In the meantime, make sure to read up on PHP if you don't feel too comfortable with the language. A basic understanding of variables, functions, classes and objects is needed before we get our feet wet.

There are 2 comments for this article

Hasanga on Dec 19, 2008

Hi,

Thanks for these tips. Me and my friend is planing to write a in-house MVC php framework which will help us to easily manage our development process and manage the consistency of our code.

I found your blog posts really helpfull for us. I hope you will be writing this series of articles to more further.

For those who need to know more about MVC here is a link, http://www.tonymarston.co.uk/php-mysql/model-view-controller.html

Thanks again Jonas!

Mohammad Naji on Feb 7, 2009

Hello and thanks.

If would be better if you put a link to http://www.socialgeek.be/blog/read/mvc-in-php-part-1-getting-the-feet-wet in current article.

Thanks again :)

Add a comment

Where do we go now?

You can browse through the recent articles or go to the archive for older items.