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. ...
That's an easy one :). Because the traditional way of php-coding is just not maintainable, especially for large projects.
cakePHP is just fine, the purpose of this blog entry is to show how you can use MVC in php, that's all...
You can download the code here
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]
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 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
foreach($controller->articles as $article): ?> endforeach ?>
'/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 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.
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 :)
thx !