Page 1 of 1

PHP MVC

Posted: Thu Feb 09, 2006 2:28 pm
by Todd_Z
I need a quick and dirty [but comprehensive] explanation of MVC -> any got any good references [or short books]?

Posted: Thu Feb 09, 2006 2:35 pm
by feyd

Posted: Thu Feb 09, 2006 3:45 pm
by matthijs
Yesterday I came across these (3) "More Stupidly Easy MVC in PHP" http://codesnipers.com/?q=node/233&title=

Posted: Thu Feb 09, 2006 9:04 pm
by neophyte
Thanks for the post Matthijs. Good article.

Posted: Fri Feb 10, 2006 1:48 am
by matthijs
Another good explanation I found is on phppatterns:

part 1 http://www.phppatterns.com/docs/design/ ... er_pattern
part 2 http://www.phppatterns.com/docs/design/ ... _version_2

If you find some good resources yourself please let me know, as I'm looking into these patterns as well. Never know what brilliant writings I'm missing.

Posted: Fri Feb 10, 2006 2:08 am
by cokacolarolla
Heres an onlamp article http://www.onlamp.com/pub/a/php/2005/09 ... intro.html

also found this gather detailed introduction from php.MVC: http://www.phpmvc.net/docs/guides/guide ... -101#INTRO

Posted: Fri Feb 10, 2006 11:56 am
by Buddha443556

Posted: Fri Feb 10, 2006 12:55 pm
by Christopher
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:

Code: Select all

Presentation
      |
      V
    Model
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.