iiirxs iiirxs - 3 months ago 10
PHP Question

MVC Architecture: Should data passed to view from controller be read-only objects?

I read somewhere that one should not pass plain model objects to view, but only read-only objects. So, currently I have something like this in my controller:

$user = new User(); // Model class
$user->loadUserById($id);
$this->setData('user', $user); //obviously $this refers to the controller object
$this->displayView();


And then, I render the fields I want in my template. Is there something wrong to this approach or it is totally acceptable to pass model objects to view?

Answer

I guess the reason to avoid passing a model object is, someone who is working on the view may be tempted to do some "business logic" in the view. That would break "separation of concerns".

You don't have to worry about it if:

  • You work alone.
  • You work in a smallish team of disciplined developers with plenty of communication.
  • You use a template language like smarty to render views. In accepted practice, such a language lacks the power to do business logic.
Comments