MVC (Model View Controller) for Beginners

Model View Controller or MVC is not an programming language nor API nor an Idea nor a framework, its just a concept. MVC is a programming or architectural paradigm for programming. It means that you will build an application in a certain way, or this is the way of structuring your web application and also separation of concern on how to process the information.

So basically, MVC is just an architecture. In other words, you will build a structure of your web development in a certain way in an MVC format or we will separate Model View and Controller.

How MVC works? Lets take an example of how a website works.



The Client will send a request to the server for example a google search, then the Server will process and tell the Database to find those keywords and the Database will search all the keywords and will tell the Server this is the match results and the server will tell the Client to display the results.

This is where MVC comes from, the  process of how we send a request to a website.

So basically, the Database is the Model, Client is the View and Database is the Controller.

Model
As you can see in the diagram, the Model is responsible for Database logic it means that all logic related to Data processing you will put it in the Model.

  • Storing or retrieving information from your Database
  • Processing information from or to the Database
  • Only connected to Controller
Controller will not directly talk to the Database, the Model is responsible for this. So the Model will access the Database and hand it back to the Controller.

View
As you can see in the diagram, the View is responsible for displaying the Data to the end user. So everything the user sees is under the View.

  • Everything the end user see.
  • All about HTML or CSS
  • Only connected to Controller
So basically, the Controller will not directly view the information coming from the Model, it will be pass to the View. The View will process the information coming from the Controller and display it to the end user or a browser. The Controller is responsible for what the View will display, so basically the View only display what the Controller says to display. The View only listens to the Controller.

Controller
As you can see in the diagram, the Controller is act as a middle man. The Controller is responsible for the server side logic. So when users send a request, the Controller knows what to do in that request.

  • Process all request like GET, PUT, POST, DELETE
  • Server-side logic
  • Middle man of View and Model
    • Get the information from the user
    • Process the information and talks to the Model (Database)
    • Retrieve information from the Model
    • Talks to the View and display the information
So the Controller gives commands to the View on how to display the information. So everything the user send a request the Controller will process it. The Controller will talk to Model, that this is what I need. So basically, the Controller is the brain of MVC, think about a commander. So the View and Model just follows instructions from the Controller

Summary
The Model is responsible for all Database logic, the View is responsible for displaying the information to the end user and the Controller is responsible for all the logic, processing the information, act as middle man of Model and View and gives instruction to View and Model.

Comments