Answer the question
In order to leave comments, you need to log in
MVC (PHP): Do I understand layers correctly?
Please tell me if I understand the layers of MVC architecture correctly (for example, web projects).
Controller (Controller) - an application layer that receives requests (commands), and gives the result (usually to the View). Those. the controller acts as a link between client requests and receiving a response.
View (View) - the layer of the application, which is responsible for the presentation of data received from the Controller (or in the controller).
Model (Model) - a layer of work with data. it is difficult for me to perceive this layer, because hardly the controller always works directly with the model?
Surely there are some intermediate layers? For example Service? And in the services DAO or Repository are already called (by the way, what is the difference between them?).
Can you please correctly decompose into layers for me, and write what role they play?
Answer the question
In order to leave comments, you need to log in
Model - the actual data with which the program works. They form the basis of the program and, generally speaking, do not need to know anything about controllers and other user interaction. Theoretically, the data layer can be accessed from anywhere - even from a script, even through rest, even through web sockets, even through the interface of a desktop application, etc. Services in which you have business logic are located next to the model and form the core of the application. Either these services do not exist and all the business logic is in the model (this can also be)
The controller is the layer itself responsible for interacting with the user (moreover, the specific method of interaction is via rest or web)
View- also nearby, these are the transformed data of the model in the way they are given to the user. If we are talking about rest - there may not be a representation as such, models can be given immediately. Or maybe - it all depends on the specific task.
Storage - the layer responsible for saving model data to the database / files / somewhere else. The model, as pure business logic, usually does not need to know how and where its data is stored. There are simply interfaces - read data, write data. And behind them is the implementation of working with the database and other storages.
This I wrote the way it is supposed to be done in theory, as described by Robert Martin in Clean Architecture. It is clear that in reality (and we are talking about PHP), all this is unlikely to be so clearly separated in the code. But in my head there is such a division into the Controller (view) - Model - Storage, I advise you to keep it.
Model (Model) - a layer of work with data. it is difficult for me to perceive this layer, because hardly the controller always works directly with the model?
Surely there are some intermediate layers? For example Service? And in the services DAO or Repository are already called (by the way, what is the difference between them?).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question