1

Welcome,
I'm writing simple Mvc framework for my own usage and I have this problem. In my framework I distinguish different types of view depending on output document type. For instance, I have htmlViewNews and xmlViewNews.
XML type of view just displays list of news as XML document. But HTML type of view apart from displaying list of news, should display menus, list of tags and many different stuff that require getting data from model.
I think it's a bad idea to do actions that depends on type of view in controller because it's not oop way.
But is it ok to direct access models from view? I thought of something like that: Make decorator for all types of view. Ex. htmlViewFrame and xmlViewFrame with direct access to models which would display all stuff like menus. Other but similar solution is to make htmlViewFrame base class for htmlViewNews.
Is it ok from mvc point of view?
Third solution, probably the best but most complicated, is to send signal from view that data about menus are required. main controller who received signal would run sub-controller which would access model and send back data to view. But is it worth to complicate things like that?
Sorry for my bad English.

  • Yes, it is quite all right to access model layer from your views. Also you might find [this](http://stackoverflow.com/a/16596704/727208) and [this](http://stackoverflow.com/a/5864000/727208) relevant. – tereško May 31 '13 at 10:39
  • Note that I'am not saying about passing model as argument in view's method, but creating and accessing instance of model using framework interface inside of view class. But if it's ok, then that's what I'll do. Thanks for help. – Dawid Garus May 31 '13 at 13:24

1 Answers1

-1

I would suggest:

  1. Keep a flag (for instance URL part, session variable, cookie) that tells to the controller/action which view to draw (html, xml, json, etc). I assume that you have actions that can decide which view to draw.

  2. The menus should be handled from a higher decorator pattern entity. Maybe the "layout" or the "MenuController".

  3. The tags, if they are part of the news article should be handled inside the action. Otherwise GOTO 2.

  4. Don't allow the views to have direct access to the models :)

aletzo
  • 2,471
  • 1
  • 27
  • 31