-1

I'm currently studying the MVC pattern for PHP but am confused about this aspect:

Is the controller responsible for getting data from the model/s and passing to the view or should the view retrieve data from the model/s directly?

Here are my perceived advantages for each setup:

Controller passes data to view

1) Makes sense that since controller is solely responsible for updating models, then it should also be solely responsible for querying from them

2) In relation to 1), model instances do not have to be injected to views, just the actual data, hence less/no coupling between the two. Controllers are the only components that are "application-aware" as models and views are simply reused/plugged in different applications

3) Views can easily be switched especially if same data is displayed (e.g. different theme/design for the Product page). The controller retrieves the same data from the models and passes to the view regardless of the theme.

4) Views can easily be packaged as HTML files and compartmentalized for the design team while controllers and models are packaged as classes for the development team

View gets data directly from the models

1) Less coupling between controller and view as controller doesn't need to know beforehand data view displays exactly; controller just passes UI input

2) Related to 1), distinctly different views/templates can easily called by the controller with no concern to it since views are responsible for getting whatever data they needs themselves. Consider two versions of the homepage or admin console. Each version can contain different kinds of data and hence, would need different sets of models or model calls depending on which version is used.

3) Views/templates are easier to customize and reuse if you package them as classes with optional setter methods to modify their appearance. If not, each view will have to be split into two files: a) view logic (in PHP) and b) view template (HTML tags with minimal PHP variable displays).

If possible, please provide a framework-agnostic response. Thanks!

alds
  • 525
  • 9
  • 19
  • 1
    For asking a question, you sure as hell seem to think you know the answer. Furthermore, the whole point of MVC is for the view to not directly access the model: therefore your second example "View gets data directly from the models" is flawed on it's face – Dexygen Feb 03 '16 at 14:26
  • 1
    everyone has different opinion on MVC, so its hard to say. I can only point you to link http://stackoverflow.com/questions/1973221/can-i-call-a-model-from-a-view. to me MVC is built upon about SoC. nonetheless, there are many SO posts about this. – Andrew Feb 03 '16 at 14:29
  • @George Jempty. Why would I waste my time asking a question if I know the answer already? Many of the articles available touch only on the theory and basics of MVC. I've been around long enough to understand some (but not all) implications of each case, but don't have the experience in practical implementation to lean one way or the other. Keep your snide remarks to yourself if you find it contemptible to guide people less smart than you. It's not helpful. – alds Feb 03 '16 at 17:11
  • @Andrew. Thanks for the link – alds Feb 03 '16 at 17:17
  • @alds whatever, use Google and Wikipedia first, rather than clogging up SO with a crap question – Dexygen Feb 03 '16 at 19:57
  • as per my understanding, controller doesn't pass data to the view, but rather alter the state of view – Andrew Feb 09 '16 at 14:08

1 Answers1

0

Traditionally, the controller ferries the data from the model to the view. Also, if you follow traditions, the controller should not transform the data from the model, as the model itself should be doing all of the logical stuff, the controller is just meant to set the stage. The view should contain zero logic if possible. If the view contains logic of its own, it becomes more difficult to unit test. Template frameworks like handlebars and twig purposely make it harder to inject your own logic, however they do typically provide some flow control, so I guess you cant say they are truely logicless.

user3661841
  • 428
  • 3
  • 13