I saw many resources online with different graphics about MVC architecture. Some of them show that the View component passes the presentation to the Controller and the Controller provides the user with the response; like this: first graph
The other ones show that the View component directly presents the results to the user without interacting with the controller; like this: second graph
This confused me a lot, especially after seeing 2 articles on GFG, each one explaining MVC differently. So which one is correct? if the first case is correct, then who is responsible for the rendering?
PS: I am working on a laravel project and need to know this information.
6
Answers
Normally, it should be the controller, but simply think of of who and where the
return
statement is being called. and thats the one who passes the response back to the requester, but the actual response structure is handled by laravel router, its the one that constructs the actual response headers and bodyHere’s an example you can argue with your ideas and who passes the response
But normally in Laravel, you should pass the route to a controller which process the data then make a return statement, which you could say the controller is the one who make the response and if you want to return an html body then you should call the view method to construct the html structure.
i.e.
Then your controller
Take a loot at here
Its safe to assume like this
who passes the response back to the user? – Its always the router that handles eveything be it a string, json, html, files, exception etc. its the one that handles the response headers and body
who made the response? – whoever has the return/render statement or whoever throws an error.
does view passes the response directly back to the user? – always No, everything is pass back to the router which process the response. view is only a wrapper for dynamic html formatting. with the rise of front-end framework and even with laravel internal support for react and vue, view will never be use unless you use blade template
Update
In this article on the Model View Controller, it says in part:
Nowhere is mentioned which type of object sends the presentation to the user
In reality both diagrams are technically possible, but the second diagram, which unfortunately lacks adequate description, is potentially less misleading. Data, typically in the form of a model object, is sent by the Controller to the View (there is another possibility, which will be described) and then the View uses that data to create the presentation that will be sent to the user. The View then typically sends the data to the user but it can also return it back to the Controller who does the actual sending. Only in that sense is communication between the View and Controller bi-directional. But in the end it is the View that has the responsibility of creating the presentation.
Let me elaborate:
Controller
Model
A model object typically represents some entity in the problem domain, such as a customer, an account, etc. or a result generated from the execution of a use case. It performs the create, read, update and delete (CRUD) operations (data logic) on data that gets encapsulated as its state and exposes methods (business logic) that operate on its state.
View
I have already said much about the View in discussing the Controller, so please excuse any repetition here.
A result obtained by the controller generally needs to be "viewed" by an actor. Sometimes the controller passes dats to the controller, quite often as a model object, and the view then presents it in some format to the "user" (actor). For example, in a web application a view object might transform the model object into HTML and then send the HTML to the actor’s browser or return the HTML back to the controller, which does the actual sending of the HTML to the browser. Or there could be multiple views on a single model object that are sent to the user, which occurs frequently in GUI-based application. In this case the view objects register themselves as "observers" of a model object and are notified whenever the model object has been updated. The view objects then retrieve the updated model data and use it to update the presentation with no interaction from the controller.
Simply every interact with user is in controller.
As you get request call a controller function.
When you want to return a produced response either view or JSON or anything you do it in controller!
So your answer is controller. And you see it in both diagrams.
In first one controller gets view and sends.
In the the second one controller sends view to user and user interacts.
In the Model-View-Controller (MVC) architecture, it is the responsibility of the controller to receive user input and determine the appropriate response to send back to the user. The controller is responsible for processing and interpreting the input, making decisions based on the input and the application’s logic, and then updating the model accordingly.
Once the model is updated, the controller then passes the updated data to the view, which is responsible for rendering the response to the user. The view receives data from the controller, formats it appropriately, and then presents it to the user in a way that is visually appealing and easy to understand.
Therefore, while both the controller and the view play important roles in the MVC architecture, it is ultimately the controller that passes the response to the user.
It is always the controller, and it has to be the controller only. It is a basic principle of MVC. You should always hold yourself to the rules and sending response objects through the controller only.
Something for your knowledge
In Laravel, we do not need a
Controller
also. Because they have introduced a mechanism called Route Model Binding, you can return data without controller support.Example
Other than what I found, this was more accurate and detailed to the Laravel Life cycle hook explaining proper MVC.
Hence, when dealing with API(Laravel as API, Vue, React, Angular), we will no longer see View.
Read