Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

General on mvc... should controller pass data to view or view should grab it directly from model?

I’m trying to learn and fully understand mvc pattern and learn php at the same time. I decided to built basic mvc framework that I could use on various projects later on. Having read lots of posts in here regarding mvc and coupling between models/views/controllers I’m a bit lost.. At the moment my understanding is that in web application controllers deal with coming request from browser and, if necessary, calls methods on model classes telling models to change its state. Then controller instantiate appropriate view class that will be responsible for displaying interface. Here's the bit I don’t understand...

  1. Now should controller pass appropriate model object to view and view should pull out all the data from model when needed?

  2. Or controller should grab data from model and pass it to view, possibly wrapping it all into single wrapper object that view will access and grab data from there?

  3. Or view should simply instantiate appropriate model when needed and pull out data directly from model object?

From what I read here

http://www.phpwact.org/pattern/model_view_controller

I’d lean towards the 3rd option where controller doesn’t pass anything to view and view instantiates model it needs. This is because:

  1. view and controller should have same access to model

  2. controller shouldn’t act simply as mediator in between view and model.

Is there really one correct way to do it or it rather depends on project? Also what approach would you recommend to someone who has decent understanding of OOP but is relatively new to php and not too clear on mvc architecture. Or maybe I should go with whatever seems right to me and learn from my mistakes (would like to avoid this one though ;)?

Now, please let me know if my question is not clear will try to better explain then.. Also I read lots of posts on stackoverflow and numerous articles on different sites, but still would appreciate help so thanks in advance for all answers.

like image 685
spirytus Avatar asked Sep 23 '09 03:09

spirytus


2 Answers

Web MVC and Desktop MVC are two very different beasts.

In Web MVC, a link in a View calls a method on a Controller, which updates a Model, and then redirects to an appropiate View, which opens up a Model and shows what it needs.

In a Desktop MVC, option 3 is wrong because both the view and the model should use the same reference. In Web, there's no choice.

Option number 2 is not MVC. It's MVP, wherein the Presenter is a mediator.

A Controller has Write-Access to a Model; a View has only Read access.

like image 140
Tordek Avatar answered Oct 31 '22 04:10

Tordek


Personally, I've always been a proponent of #3. The view shouldn't care about the controller. The view shouldn't have any dependency on the controller for that matter. It should do what it's supposed to do, show a view of the model.

The basic flow of control should be thus: The controller receives a request from a browser. It makes any updates to the model, that is relevant, and then selects a view. The control is then passed to the view, which gets data from the model and renders it.

As an extension, user input can be consider part of the model, and both the controller and the view may read from it. The key point to take away is that Controller and View should have no dependency on each other. That's why the pattern is called MVC.

Now, personally, I find MVC a bit too tedious, and so I usually conflate Controller and View more than this. But then that isn't really MVC.

like image 40
troelskn Avatar answered Oct 31 '22 02:10

troelskn