This is an architectural / design question that just came to my mind when comparing current MVC practices in PHP with component based development in Javascript.
My question is:
Why doesn't the view (or parts of the view) define what data should be used to display it?
It seems to be common practice in MVC Frameworks to let the controller define what data the view needs. For me this creates unnecessarily coupling between the controller and the view.
When the view library has the possibility to call functions (twig for example) which retrieve the needed data structures, why wouldn't that be preferred? The controller would only have to know which root view to render and parts of the view would then work with the model to read the necessary data to render itself. I see following advantages:
- Controllers get simpler as they don't aggregate the data before rendering the view
- Unused data structures passed to the view would occur a lot less
- Re-using parts of the view (templates) would also be easier as they aren't dependent on the controller to render anymore
Obviously when changing data the logic would stay in the controller, but I'm referring only to reading data to display the UI. Certain data (e.g. after submitting a form) would still have to be passed to the view by the controller, but most of it could be put into independently renderable components.
Do you see any disadvantages to this approach? Have you already tried something similiar to this with an MVC Framework?