Model View Controller is something I feel like I need to address for not only my sake but for any new Web Developers out there. So… What is a model view controller? Its a software design pattern! It’s used for developing user interfaces that divides each specific action. This pattern is commonly used to help create more complicated applications. Each function works together and distributes to the user.
So let’s start on how it works. The user sends a request, that request immediately gets sent to the controller. The controller itself handles all request flows and is basically the middleman of the operation that interacts with both the view and model. The controller usually doesn’t hold too much code itself. The controller then requests from the model for any data that needs to be retrieved.
So now for the model, the model handles the data logic, and interacts with the database itself, think about it being the warehouse worker getting things ready for the front end people or the floor people. After handling the data logic the model will then send back data to the controller. Now the controller has to get a presentation ready, so now the controller has to go to the view to get a presentation.
Now the view handles the presentation using HTML and CSS. The view will only send back on what it has been requested by the controller. The view and model never interact with one another. When the view is done with prepping a presentation, the view will send back a presentation to the controller. This is where the controller will have the responsibility on sending back the user a response once everything has been checked out.
Let’s help solidify what we’re talking about with an example. Let’s say the user requests some sneakers too look at.
- This is where the controller receives the request and asks the model for a request of all the sneakers. The model will then process the request and look for a list of all sneakers in the database.
- Now the controller receives the information given from the model, if successful the controller will now ask the view to return a presentation of sneakers.
- The view will take that information and render it in HTML. With everything being ready for the presentation the view will then send back the presentation back to the controller.
- Since the process went successfully, the controller will now return the user a list of sneakers as requested. Which will ultimately end the request.
- Let’s look back at number two. What if the request of data from the model was unsuccessful? Well this is when the controller has to take that information and go back to the view.
- View will then be asked by the controller to create an error presentation in the form of HTML to be ready to be sent to the user. Once completed the controller will tell the user the error that was made.
Let’s break it down one last time to the bare minimum information to simplify the whole thing. The model handles all the data, the view handles all of the presentation, and the middle man being the controller, tells both of them on what to do.
Great! Thanks so much for reading! It’s not the most exciting or complicated thing about coding but something I felt that needed to be addressed. There’s so many things to coding that it’s always nice to come back to basics and solidify the foundations on when we first learn to code. Have a great day! Keep coding on!