Model View Controller Pattern (MVC)

What is MVC?

  • MVC stands for Model-View-Controller, a common pattern for architecting web applications.

  • As well as a term that we use very commonly in the industry, for indicating which parts of an application we are currently managing or touching.

  • The Model-View-Controller pattern is a way of Thinking of our application in terms of three layers.

Describes the 3 layers of the application we are developing
3-Layers:

Models:

Manage data and business logic for us.
What happens inside models and databases, capturing logical relationships and properties across the web app objects.

Views:

Handles display and representation logic.
What the user sees (HTML, CSS, JS from the user's perspective).

Controllers:

Routes commands to the models and views, containing control logic.

Control how commands are sent to models and views, and how models and views wound up interacting with each other.

Explanation:

  • The Models layer manages data and business logic for us.
  • This is encapsulated by what happens insides of our models as well as our database.
  • It captures logical relationships and properties for all of the objects in our web app.

  • Our Views then take the data and models that we have and handles how we should represent and display those data to our users, representing the user experience and user interface design that we show in our web clients.

  • Our Models and Views don't directly talk to each other, however, they talk to each other through controllers.

  • Controllers control the logic of how commands are sent to models and views, and how models and views wound up interacting with each other to update one another.

  • So when a user visits a website, what they see is the view.

  • The view is that an HTML file that contains CSS and JavaScript, allows you to interact with the web app from the user's perspective.

  • When a user clicks on information or interacts with that web page, that can sometimes send requests back to the server.

  • Those requests are then routed to a particular route and then sent to a controller.

  • So the controller processes the user requests, and then at times notifies the model which then manipulates the data that powers the application, including doing reads and writes to and from the database.

  • At times, the controller can ask the model for certain types of data, or to do certain things to the database, and based on the responses coming from the model then inform what the view should show next.

  • At times the controller could tell the view to redirect to a different page, or show something else instead, and other times updates to the model can update the view because the view is bound to data that exists on the models via the controllers.

16