Reputation: 4582
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?
Upvotes: 1
Views: 101
Reputation: 58444
This is kinda complicated.
The recommendation to provide "read only objects" (they probably use a word "immutable") is a slightly exaggerated conclusion from a larger principle regarding MVC:
Views should not alter the state of model layer.
Changing the state of the model layer is the responsibility of controllers. That said, controllers are not responsible for passing information to the views in MVC. That's what ViewModels are for in MVVM and Presenters - in MVP.
I would also note that you should not confuse "views" and "templates". A view utilizes templates, but it's responsibility of to produce response that reflect the sate of model layer. You might find this interesting to read.
Upvotes: 1
Reputation: 5183
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:
smarty
to render views. In accepted practice, such a language lacks the power to do business logic.Upvotes: 0