简体   繁体   中英

How do you decide if something goes in the view or the controller? (Zend Framework)

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM