I'm playing with MVC in PHP. I'm not using a framework, I'm just trying to understand the pattern.
Sometimes I see controllers, for instance in this tutorial, that are instantiated with Models and Views passed into the constructor, and in the same tutorial the view ( here 'Template' ) class, takes a Controller in the constructor!
So my question is:
Given the many variations and misconceptions of MVC on the web, I voted to close the question as Not Constructive. I provide this answer as a Community Wiki, because of the limited comment size.
MVC was originally conceived by Trygve Reenskaug for Desktop Environments and not for the web. What we see nowadays are mostly variations of the original pattern in some form, like MVP, HMVC, Model2-MVC and so on. Their implementations differ from each other and people spent lots of time quibbling over what "MVC" really is or how it should be implemented.
Personally, I prefer the Patterns of Enterprise Application Architecture definition of MVC which says MVC splits user interface interaction into three distinct roles, because this definition has no notion of implementation.
In the book, Fowler notes that the most important distinction is the separation of the Model from the Controller and View. While the book does cover the associations between the roles to get that argument across, I think the main point is to understand and focus on the roles and not the implementation.
Design Patterns are blueprints, not detailed schematics. Also, MVC is a Pattern Language. It's aim is to add structure/form to a project. Understand what the roles are for and then come up with an implementation that works in your project.
Fowler also has a lengthy article on GUI architectures covering MVC explaining the associations and dependencies in details, in case you don't have the book handy.
Images/Slides from: Architecture the Lost Years (Robert Cecil Martin; 4 Nov 2011)
If you have user inputs, you need to know where to send them.. to what controller.
I don't like having my controllers play with models directly, because controllers are exposed to the "outside world", and prone to attacks. You can have a look at service layer pattern, Data Transfer Object pattern, etc. I like to have my models isolated behind a service API that controllers will use. If you need a good book about patterns, you can look for Martin Fowler's :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With