Since i already managed to extensively write about model in context of MVC and PHP, lets just focus View
and Controller
parts of the triad.
As @Gordon already mentioned that, what we do in the Web, is not classical MVC. It's not rally possible. Instead we have come up with distinct variations on original idea.
View is not a template
Unless you are using a particularly bad implementation of MVP, the view
instances are objects, responsible for presentation logic.
Whether controller
instance needs access to view
depends on which MVC-inspired pattern you use. In MVP and MVVM patterns controller
instance requests information from model layer
and passes it to view
(either with some modification or additional flags).
In the Model2 pattern (which is somewhat closer to original concept), the view
instance itself is able to pull information from model layer
. In this case there is no need for controller
instance to have access to it.
What is controller responsible for ?
Instances of controller
should not be directly instantiating structures from model layer
. There are two main reasons for it:
- causes tight coupling to particular class names, makes for harder testing and maintenance
- adds additional responsibility (or reason-to-change) and thus - violates SRP
Initialization of structures from model layer
is complicated. They usually require you to inject database connection, cache handlers or even other structures from model layer
. This task should be left to a separate factory-like instance (shared between controller
and view
instances), which controller uses.
The main responsibility of controller
instance should be changing the state of model layer
. It should take input from user, and translate it in the form, that is understandable to model layer
.
Keep in mind, that model layer
as a whole must be completely ignorant about view
and controller
instances.