PHP MVC

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

PHP MVC

Post by Todd_Z »

I need a quick and dirty [but comprehensive] explanation of MVC -> any got any good references [or short books]?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

Yesterday I came across these (3) "More Stupidly Easy MVC in PHP" http://codesnipers.com/?q=node/233&title=
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Thanks for the post Matthijs. Good article.
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post 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.
cokacolarolla
Forum Newbie
Posts: 1
Joined: Fri Feb 10, 2006 12:53 am
Contact:

Post 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
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
Post Reply