Getting to Know Model-View-Controller (MVC)

Words
792
Reading
4 min
Listen
Play
9y

model-view-controller-mvc-explained.jpg
Source

Hello guys, its me casweeney@casweeney again welcoming to another wonderful topic in the programming world. Today or in this article I will be talking about Model View Controller.

Model View Controller (MVC) is not a framework, it is an architectural design pattern or programming structure. A lot of people call it a framework, no no no it is not a framework I repeat, it is an architectural paradigm. This simply means the structure of your programming which is gonna be built in a certain way i.e the MVC.

MVC is also a way of thinking and a way of structuring your application to communicate with users.

Why is MVC Popular

The major reason why MVC is so popular is because it is used by many frameworks, this why people often get confused, calling MVC itself a framework. Now some of the popular frameworks that uses MVC are: Ruby on Rails, Cake PHP, django, Laravel, Zend framework, CodeIgniter, IOS.

Now the reason why this article is very important is because for you to work with any of the above listed frameworks, you have to understand MVC and how it works

What is MVC?

  • M: Model
  • V: View
  • C: Controller

Reason why you need MVC

  • Never repeat yourself: It prevents programmers from repeating the same code over and over again. As a programmer, there are some things we will have to work with numerous times in different areas of our code, with MVC, we don't have to repeat such codes over and over again.

  • Structured Programming: It helps create a solid structure around our web application. Remember when we write all the codes on the same page but using MVC, the codes are separated thereby giving your app a well defined structure which is like an engine that has different compartments.

The MVC flow

Lets say a user working with his laptop is requesting something from google, he will be using one of the Client(Chrome, Firefox, Internet Explorer) which is simply processing and rendering HTML/CSS/JavaScript which may also be referred to as client side languages(JavaScript) HTML/CSS(Markup languages). All this are being processed by the browser.
Capture.PNG
Source

So when a user is searching for some thing on Google, a request is sent out to the Google server which may be running with Linux or Windows. The server is where the information sent by the user is being processed according to the instruction given to it using server side languages like PHP, Ruby and Python. The server itself does not store the information that was requested by the user, it only processes the command and makes a request to the database where the information is stored over long period of time. The database can be in MySQL, PostgreSQL, NoSQL or MongoDB. The database receives the command from the server, search for possible results and send it back to the server which processes it one more time and send it back to the client in pages in form of HTML and CSS and the clients web browser processes it and renders it to the screen and all these happens in a fraction of seconds.

Note:

The above explanation happens regardless of MVC, that is the normal flow of information from a user to the database.

Now lets see how MVC comes into play

mvc.PNG
Source

Using the above flow, the MVC was built in the same flow chart in such a way that app will have three sections or categories whereby one part deals with the database, the other part deals with the server and lastly the part that deals with the view, where clients view the output. Let's explain further using the following breakdown:
Model = Database : This contains codes that are related to the database section of your app. This is anything you do to interact with your data i.e taking things and sending things to the database. The model is actually connected to the database.

View = Client : This is where the front-end visible codes are housed. This is what displays information to the user. It is traditionally HTML and CSS. Anything you see on a website is on the view aspect of MVC.

Controller = Server : Here contains codes that processes information that goes to and from the database which is later sent to the view to be displayed. The controller never talks to the database, it talks to the model and the view. The controller is the middle man between the view and the model. Hence, the Controller contains all the chunks of codes.
It is the man in charge.

Conclusion

Change they say is dynamic and the programming world is not left out. Go with this change or get left behind.

Cassy Steem.gif

Getting to Know Model-View-Controller (MVC) | Ecency