0

So, just something I was wondering..

Say I have a model called Comment which has a function called get() to fetch all the comments in the database. According to the "true MVC pattern", would it be allowed to call it directly in the view like this?

<html>
    <?php foreach (Comment::get() as $comment): ?>
        <h3><?= $comment->title ?></h3>
    <?php endforeach; ?>
</html>

Or should you always pass an array of comments through the controller like this?

Controller:

return $view->assign('comments', Comment::get());

View

<html>
    <?php foreach ($comments as $comment): ?>
        <h3><?= $comment->title ?></h3>
    <?php endforeach; ?>
</html>

I just find accessing the model functions directly in the view like the first example just more convenient sometimes. As far as I know, the view is still just taking care of the presentation bits and the model still does its job by getting those comments.. but I am not sure if it still is according to "MVC rules" :P

Gladen
  • 792
  • 2
  • 8
  • 17
  • 3
    Yes, kinda, but the "model" is not what you think. [This](http://stackoverflow.com/a/5864000/727208) might help. – tereško Aug 07 '14 at 20:33
  • @tereško Thanks for that helpful link. I never looked at models like that. – Gladen Aug 07 '14 at 20:53
  • In addition to @tereško great comment: `view != template`. The controller and view share the same model, but they don't know each other. – Christian Gollhardt Aug 07 '14 at 22:03
  • No it is not best practice. It is important to keep your controller thick, so all processing is done there. It is neat and organised that way. – tread Aug 07 '14 at 22:40
  • 1
    @StevieG actually it's the exact opposite: the controllers should be as thing as possible. Controllers should be responsible for altering the state of model layer and in rear cases (usually in desktop apps, where you have persistent views) alter the state of view. – tereško Aug 08 '14 at 04:22

0 Answers0