MVC - should the View be given access to the Model?

7 views
Skip to first unread message

sunwukung

unread,
Aug 19, 2010, 4:46:59 PM8/19/10
to PHPSW
Hi all
I thought I'd try and kick off a discussion I've been having in a
forum elsewhere, and see what people's thoughts were on this subject:

I recently built my first app in Zend, and as with all first attempts
it's riddled with code reflecting that learning process. I generally
accessed my Model classes from the Controller invoked by a given
request, and then set variables in the View according to my results.

More recently I've rolled my own MVC-lite based on that experience,
and there I pass the Model directly to the view, and using ViewHelpers
to format data at that point. This has really opened up my use of the
MVC pattern, allowing me to widget-ize the user interface and it also
makes AJAX a lot easier

//in the controller
$this->view->form = new FormObject

//in the view
echo $this->ViewHelper->generateFormMarkup($this->form->processInput)

//intercepting AJAX request in controller initialisation
$xhr = (isset($_SERVER['HTTP_X_REQUESTED_WITH']))?true:false;//detect
ajax request
$form = new FormObject;
$viewHelper = new ViewHelper;
echo $viewHelper->generateFormMarkup($form->processInput);
return;


What are your thoughts on the matter?

m...@zioncore.com

unread,
Aug 20, 2010, 4:53:11 PM8/20/10
to PHPSW
Ask a dozen devs that question, you'll get a dozen answers :-)

I say yes, a view can access a model directly.
Although I wouldn't want to change model-data from the view: read-
only, and for presentation only.

In practice, I find going through a model makes it more manageable
long-term, so I generally route through a controller for pretty much
everything.

Marcus
Reply all
Reply to author
Forward
0 new messages