Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do my MVC components really all need references to each other? [closed]

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:

  1. Why might the view need a reference to its controller? Shouldn't the view be the passive partner in this relationship?
  2. Should a controller ever have an internal reference to a particular model? Or to put it another way, why not just instantiate models in controller actions and use them that way?
like image 642
djb Avatar asked Dec 07 '22 14:12

djb


2 Answers

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.

enter image description here

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.

enter image description here

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.

enter image description here

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)

like image 175
3 revs, 3 users 67% Avatar answered Dec 10 '22 13:12

3 revs, 3 users 67%


  1. If you have user inputs, you need to know where to send them.. to what controller.

  2. 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 :)

like image 20
PEM Avatar answered Dec 10 '22 13:12

PEM