How to 'Model-view-controller' in PHPSection: PHP

This is a quick'n dirty way to use MVC in php.
In short: A design pattern that aims to modularize an application into 3 parts. The model represents the data for the application; the view represents the presentation; and the controller ties these two together and deals with user input. ...


I'm not gonna explain MVC more than that, there are plenty of resource out there explaining what mvc is.

So, why MVC?

That's an easy one :). Because the traditional way of php-coding is just not maintainable, especially for large projects.

Why not cakePHP?

cakePHP is just fine, the purpose of this blog entry is to show how you can use MVC in php, that's all...

Let's dive into it...

The code

You can download the code here

mod_rewrite

We will be using mod_rewrite for apache, as it is a great way to make your url's prettier.
Our url's will be like this: http://domain.tdl/[controller]/[action]/[id]

The controller

In app/controllers/default_controller.inc.php, there is a class named 'default_controller' (duh). All your other controllers must extend this base controller class. If you need any variables or functions to be available in all your controllers, this is a good place to put them.

Each function correspond with an action in that controller. so, http://domain.tdl/articles/index will execute the 'index' function of this articles-controller. If you want an variable to be available in the view, make that variable an instance-variable of the controller-class (use $this->variable). In the view, the variable will be accessible with $controller->variable. This is how a controller looks like:

5));
	}
	
	public function show(){
		$this->article = Article::find($_GET['id']);
	}
	
	public function create(){
		$this->article = new Article($_POST['article']);
		if ($_POST){
			$this->article->created_at = date('Y-m-d');
			$this->article->save();			
		}
	}
}
?>

The view

The view is where the html goes. You can still use some php to loop through arrays and such, but alle the business-logic should go in the controller.

Articles

Read more

'/app/views/layouts/application.tpl.php' is the global template of your website, the 'masterpage', as they call it in ASP.NET. The variable '$contentpage' will will be replaced by the content of the current view.

The model

The model is where all the data goes, and it's the model who's making all the database call's. in 'app/models/model.inc.php' there is the base model. All models should extend this one. In this model, there are some functions like find(), save() and destroy() to interact with the database.
This is how a model looks like:


Every model needs to have at least the find, save, destroy and validate functions (enforced by the iModel interface) You can use the $cant_be_blank array to specify what fields are required. When a model tries to save, it will automatically generate some errors when there are required fields left blank. A model must also have a $table_name variable. This is the name of the table in the database.

Conclusion

MVC is a great way to code. It helps adding consistency in your application. I you still code php the usual way (mixing code and layout), I recommend trying this approach, you'll see, its easy, and much more fun!
This is just a simple explenation of what the code does. If you have any questions, don't hesitate to contact me! I hope this code is usefull for some of you :)

Reactions

Caroline (www.caroma.be)
Thanks a lot for this explanation! I needed this for a school project, so it's very helpfull to me.
thx !
Sections 
- PHP (1)
- General (2)
- Ruby on Rails Snippet (3)
- Ruby on Rails (13)
- HTML (1)
- Css (1)
- regex (1)
- Photoshop (1)
- Leopard (1)
A little note to Internet Explorer users:
This site is not made for Internet Explorer. Some pages may look like shit. Yes, i could fix it for IE, but no, I'm not gonna do it. Try Opera or Firefox instead!