Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC for dummies: why does controller have to send anything to views?

If I get this right than function of the Controller is processing POST data and technically making changes to the state of the application (e.g. DB) via Model.

As far as I understand, View also gets data from the Model.

So this is how I understand the workflow:

Client request --> App Front Controller --> (if method = POST --> Controller) --> View --> back to Client

Here Model is used by Controller to read and write data and by View to read data.

So controller is not used every time the page is loaded, in fact, only when app data is added/updated. Most of the times Controller is bypassed.

Thus, how come almost every resource about MVC is talking about Controller sending data to views?

I am trying to write an app using MVC-like pattern. So in my app views always get data for the page from the Model. When Model is updated, I add specific model update time to Memcache. At runtime each View looks up last update time(s) of related model(s) and last time cache for this view was generated. If model was updated before cache was saved, view reads cache, otherwise re-renders based on updated model.

like image 985
mvbl fst Avatar asked Dec 03 '22 03:12

mvbl fst


1 Answers

The controller is responsible for presenting views based on the data that is requested. It's there so neither the model nor the view need to know about the request. Yes, the view gets data from the model, but not always directly; the controller may have to make some decisions as well depending on the request.

It's something like having waiters in a restaurant so they can take orders from and serve dishes to customers. It's not the chefs who bring out the meals after preparing them; it's the waiters. It's not the customers who go to the kitchen asking for meals; it's the waiters who take their orders then let the chefs know what to prepare for whom. In the same way, the controller is there to handle client requests, whatever their nature may be. It's a very rough comparison though, but I hope you get the picture.

like image 86
BoltClock Avatar answered Feb 08 '23 23:02

BoltClock