views:

338

answers:

2

How do you decide if something goes in the view or the controller?

Here are some specific examples:

  • Zend_Captcha: Does the controller generate the captcha and pass it to the view or does the view generate it?
  • Zend_Alc: Does the view decide if a segment of the view should be displayed to the user or do you have multiple views depending on available actions and the controller picks the right one for display?
  • Zend_Session: Does the view keep track of who is viewing it based on the session data or is that parsed by the controller and presented to the view as a parameter of some kind?

Are the rules or guidelines for what component (model, view, or controller) should do what written somewhere where I can view them? I didn't see that in the documentation on the Zend Framework site.

+2  A: 

For Capcha you can generate in the view. For Acl use the view. Zend_Session is accessible by both - the controller and the view

Mote
I'm curious, since you said "can" and "is accessible", it sounds like this is written down somewhere. But I didn't see it on the guide pages. Did I miss it? Is it somewhere else?
Thomas Owens
+6  A: 

Generally speaking, this question can apply to any MVC framework. Here are the guidelines I use:

  1. Skinny controllers. If possible, have your controllers do little more than invoke business logic on your models and pass results to your views.

  2. Views do nothing but View Logic. Do anything related to interacting with the user visually, like generating captchas, hiding and showing links based on ACL. Don't calculate totals. Don't invoke logic on models. Don't do business logic. It is generally OK to read the session from your views to hide and show data/links. But don't rely on that for security: make your controllers secure too.

  3. Fat Models. Put as much business logic into your models as you can. That way, you can share them between controllers. If you find yourself in a controller iterating over elements of a model, resetting values based on certain rules, or otherwise doing complicated business logic, then you should try to find a way to get that logic into the model layer.

Hope this helps.

Pete
I think that helps a lot. Thanks.
Thomas Owens
I second what Pete has said. The view should just display what is handed to it. At most, you could maybe run loops in the view but that is it re: logic.
gaoshan88