You can read lots of articles on MVC and still not get to the heart of it with regards to a PHP implementation. MVC is a very high level and slightly vague pattern -- which makes it different from most of the other common pattern which are much more specific.
The strangest thing about MVC is that although it is about
three components, there are only
two separations. And MVC is mainly about separations and dependencies. The reason that there are only two separations is that the View and the Controller are in the same level -- the Presentation.
So the basic separation is the following where Presentation is dependent on the Model, but the Model is independent of the Presentation:
I should note that if you simply implement this separation (between the code that provides the data and the code that displays it) then you have 95% of the benefits of MVC. This Model/Presentation separation is the most important thing that programmers can achieve to produce better appliction code.
After that major separation there is the View / Controller separation. This is the fun part to code and it is the center of most of the discussion about MVC. However there is no agreement on where the dividing line between the two should be drawn or what dependencies there should be. In many cases it is implementation specific.
The classic form has the View dependent on the Controller like this:
Code: Select all
View -> Controller
(Presentation)
|
V
Model
Classic MVC code might look something like this:
Code: Select all
class MyModel() {
function getName()
function getAddress()
}
class MyView {
function render($model) {
$template = new Template('mytemplate.php');
$template->set('name', $model->getName());
$template->set('address', $model->getAddress());
return $template->render();
}
}
class MyController {
function execute($request, $response) {
$model = new MyModel();
$view = new MyView($model);
$response->setContent($view->render());
}
}
$controller = new MyController($request, $response);
$response->out();
PS - I would be wary of many of the articles listed. If it is PHP specific discussion I would check the source and how old the information is. Non-PHP discussions (e.g. Martin Fowler) might just confuse, but you can also get some sense of the bigger picture.