Building a simple MVC system with PHP5
(Page 1 out of 5)Abstract
In this tutorial you will learn how to build a simple Model-View-Controller system with PHP 5.1 and some of SPL's (Standard PHP Library) features.
Introduction
Welcome to the first full-blown PHP5 tutorial on PHPit, and you'll need to have PHP5.1 and SPL installed for this tutorial, because we'll be using some of PHP5's latest features.
In this tutorial I'm going to show you how to build a simple Model-View-Controller system, which is the most common design pattern for big web applications. I'll take you through all the steps necessary to start from scratch to a full-blown MVC system.
One point of entry
One of the important things about our MVC system is that it will have only a single point of entry. Instead of having several dozens of PHP files that do the following:
include ('global.php');
// Rest of the actual page code here
?>
We will have a single page that handles all the requests. This means we won't have to include a global.php every time we want to create a new page. This "single point of entry" page will be called 'index.php' and, at the moment, looks like this:
// Do something
?>
As you can see, the index page does nothing yet, but it will in a minute.
To make sure that all the requests go to the index page we will setup an .htaccess RewriteRule using the mod_rewrite engine. Put the following code in a file called ".htaccess" in the same directory as the index.php file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]
First we check if the actual file exists using the RewriteCond command, and if it doesn't exist, we redirect it through the index.php file. We have to check if the file exists, because we also want to be able to use regular non-PHP files, such as JPEG images.
If you can't use .htaccess or mod_rewrite, you will have to manually redirect the requests through the index.php file, which means all your links must be in the form of "index.php?route=[request-goes-here]", e.g. index.php?route=chat/index.
Now that all the requests are going through a single point of entry, we can start writing the index.php file. The first thing we will have to do is some startup tasks. Create a new directory called 'includes', and create a new file called 'startup.php' in the 'includes' directory. Put the following code in the index.php file:
require 'includes/startup.php';
August 24th, 2006 at 5:58 pm
Hi,
thanks for this useful article about the MVC system.
Just to report a problem using function trim in the Router’s setPath method (line 14) under UNIX systems.
If we give it an absolute path, it will delete the leading / and create problem when checking if this path is a directory.
August 28th, 2006 at 12:21 pm
Why you haven’t used the magic overloading methods __set and __get, instead of using registry class? It’s more easy to use __set, __get with static propery, because in future you don’t need to use methods get()/set(). Anyway, I’m using something like you have been described here for compatibility with PHP4. Instead of class static property I’m using container function, because in PHP4 there are no chance to declare static property in class body.
August 29th, 2006 at 9:38 am
Hello, good article about the MVC for starting.
September 4th, 2006 at 5:09 pm
Pretty cool article Dennis. Thanks. Good to see a relatively easy explanation of an MVC system. The thing with MVC is that the basics is fairly simple. But it can get complicated quickly when you throw in some locators, observers, datamappers and other classes and patterns.