6

I've been learning how to build web applications with Laravel and Vue.js and I understand the part where the user uses the view in order to send requests to the controller which then manipulates the model. I absolutely see that flow in my applications.

What I am not so sure about is why in MVC diagrams like this one: MVC pattern diagram from Wikipedia. The model directly updates the view but in my applications it seems that the Controller is the one that gets the changes from the model and sends that to the view (via HTTP).

Is there something that I don't quite understand?

  • 1
    About _"...the user uses the view in order to send requests..."_: **No**. You are using the browser, not any view. You are just "making" the browser to send a request to the server (by typing a URL and pressing *Enter*, or by triggering an ajax request, or by clicking a *submit* button, etc). Any such request is then handled by a controller. And, vis-à-vis controller, the browser is the user, not you. – PajuranCodes Sep 11 '18 at 21:05
  • @dakis Oh, so that's why the diagram says that the user is 'using' the controller and 'seeing' the view. It really took quite a while to get what you wrote but it was totally worth reading it again and again. – Rupert Raphael Amodia Sep 11 '18 at 22:33
  • 2
    Yes, that's it. Please note three things: 1) The "V" in MVC is the so-called _presentation layer_. It can be a _view_ class, but it can also be composed of a _presenter_ AND a _view_ class, or etc. 2) The _model_, e.g. the "M" in MVC, is a layer. and is composed of multiple specific classes: entities, data mappers, repositories, services. 3) The diagram is confusing. In a web MVC the model does not update any view! The _model_ should know nothing about the external world. – PajuranCodes Sep 11 '18 at 23:38
  • 3
    Instead: (a) Controller updates the _model_ and then reads the data from it, in order to pass it further to the _view_ for displaying it; or (b) Controller **updates** the _model_. The _view_ then **reads** the data and displays it. The (b) option should be choosed, even though the frameworks are using the option (a). – PajuranCodes Sep 11 '18 at 23:41
  • 2
    Here are some links with extended explanations: [this](https://stackoverflow.com/a/51450648/9455607), [this](https://stackoverflow.com/a/16356866/9455607), [this](https://stackoverflow.com/a/16596704/9455607), [this](https://stackoverflow.com/a/5864000/9455607) and, maybe, [this](https://stackoverflow.com/a/51735316/9455607). – PajuranCodes Sep 12 '18 at 11:46
  • I'd really consider your second comment, onwards, as an answer. Thanks alot! – Rupert Raphael Amodia Sep 12 '18 at 13:43
  • I see. So for (b), that's where the concept of **ViewModel** comes in right? – Rupert Raphael Amodia Sep 12 '18 at 13:48
  • Exactly. For option (b) think like this: The _model layer_ fetches data from db, for example, and returns it as objects. So, an object of the presentation layer (let's say a _presenter_) pulls these objects from the _model layer_ **as they are**. But, before it is displayed, the data must be **formatted** in a format wished by you, the user of the browser. So the _presenter_ object formats all data pulled from the _domain model_ to strings and booleans and passes it to a so-called _view-model_ object. – PajuranCodes Sep 12 '18 at 14:15
  • Then, another object (a _view_) receives the _view-model_ object and reads the formatted data from it. Then it loads and renders a specific _template file_, filling it with that values. Note that, when using a template engine (like Twig), the _view_ class will use the _"engine"_ (or _"environment"_) object of it, in order to load and render the template file. If you'll study the links in detail, then you'll understand everything a lot better. But don't hesitate to ask. Good luck. – PajuranCodes Sep 12 '18 at 14:23
  • Watch [this](https://codurance.com/videos/2015-04-24-interaction-driven-design/) too. – PajuranCodes Sep 12 '18 at 14:31

1 Answers1

2

In this diagram view is a representation of model. when model changes view changes to represent the model. in a real MVC application controller may send model to view (this model is called view-model which is a special type of model and it may get populated dynamically by controller using back end model). Some MVC applications have two types of models a model (which may be a database for back end) and a model (called view-model which is for representation and will be send to view). For example ASP.NET MVC has this type of view-model.

What is ViewModel in MVC?

https://en.wikipedia.org/wiki/Model–view–viewmodel

ygngy
  • 3,630
  • 2
  • 18
  • 29
  • Not really an answer that I was expecting but wow this just made me realize something. This means that I can create classes for views and even extend (through composition or inheritance) those classes. Wow! Never thought about that really. So that's how Laravel Nova is structured, through **View Models**. Thanks a lot, BAHMAN! – Rupert Raphael Amodia Sep 11 '18 at 22:39