2009年2月14日星期六

[fw-mvc] Mixing Models and Controllers

Hello,

I apologize if this has been discussed before or if information on this
practice is readily available (I haven't read anything relating to this
yet).

What's the best practice in terms of mixing models and controllers. For
example, I have 2 models:

Articles
Comments

When someone visits /articles/view/id/3, they see the Article of ID 3 with
the related comments and a comment form at the bottom. When the submit
button is pushed, I have it being processed in the viewAction of the
ArticleController. The action validates the form and passes the data to an
addComment() method of the Comments model where I then use Akismet to check
for spam and finally add it.

So the flow is kind of like:

Article viewAction
(article, comment, and comment form is displayed)
Form submission
Article viewAction
comment Model
Article viewAction with new comment

Instead of doing the form processing in the viewAction of the Articles
Controller, should this rather be in an addAction of a Comment Controller?

Article viewAction
(article, comment, and comment form is displayed)
Form submission
Comment addAction
comment Model
Article viewAction with new comment

I could understand both ways of doing this: Since the viewAction is already
fetching all of the related comments of the article, why not just tie in the
add action as well.

Or on the other hand, it divides responsibility more when the Comment
Controller handles adding its own.

And if I think about it more, if the preferred approach is to have the
comments processed by the Comment Controller, then should the original
ArticleController viewAction actually be two processes: one in which the
article is pulled by the ArticleController and one in which the Comments are
pulled by the CommentController and then combined in a single View?

I hope this makes sense.

Thanks
Joe
--
View this message in context: http://www.nabble.com/Mixing-Models-and-Controllers-tp22018085p22018085.html
Sent from the Zend MVC mailing list archive at Nabble.com.

没有评论: