I am aware that MVC tutorial has already been existing in many websites. However, most of them only define the topic and technically elaborate(for developers’ level) it. On this article, I will try to make a concise and well-explain guide so that even beginners can easily understand.
Let’s get started
According to wikipedia, Model–View–Controller is an architectural pattern commonly used for developing user interfaces that divides an application into three interconnected parts. This is done to separate internal representations of information from the ways information is presented to and accepted from the user.
Take note that MVC is an architectural pattern and
NOT a programming language. I just want to clarify because some of you might be confused about it. Meaning, this pattern could possibly be applied to all programming languages or maybe to most languages. In fact, MVC pattern has been used by most frameworks like Laravel in PHP, Django in Python, and Ruby on Rails in Ruby.
Real World Scenario (illustration)
Consider a scenario in a restaurant. When you, the customer(User), order a food, the cashier(Routing) accommodates your order(request). Once the order is completed, it will be encoded and processed through the system(Controller). Upon entry of order, the system will check if the menu ordered is still available or out of stock. Now, if the order is available, it will be sent to the cook (Model) for preparation. Once it is done, the waiter(View) will distribute or deliver(response) it to the customer.
Notice, the process above is similar with that of MVC pattern. Take a look at the image below:
From the scenario above, let’s define the entities based on our MVC illustration.
- User (Customer) – Just like how a customer orders a menu, site’s user sends a request to the server.
- Routes (Cashier/Counter) – The routes, like a cashier, accepts/entertains user’s requests(GET,POST,etc). After that, it will go to controller to process the request.
- Controller (Restaurant System) – serves as an intermediary between the Model, the View, and any other resources needed to process the HTTP request and generate a web page.
- Model (Cook) – represents your data structures. Typically your model classes will contain functions that help you retrieve, insert, and update information in your database.
- Views (Waiter) – is the information that is being presented to a user. A View will normally be a web page, but in CodeIgniter, a view can also be a page fragment like a header or footer. It can also be an RSS page, or any other type of “page”.
I hope everything above makes sense to you. Maybe, you might ask, why we use MVC pattern? Take a look at the advantages.
Simultaneous development – Multiple developers can work simultaneously on the model, controller and views.
High cohesion – MVC enables logical grouping of related actions on a controller together. The views for a specific model are also grouped together.
Low coupling – The very nature of the MVC framework is such that there is low coupling among models, views or controllers
Ease of modification – Because of the separation of responsibilities, future development or modification is easier
Multiple views for a model – Models can have multiple views
However, MVC has also downside or let’s say disadvatanges.
Code navigability – The framework navigation can be complex because it introduces new layers of abstraction and requires users to adapt to the decomposition criteria of MVC.
Multi-artifact consistency – Decomposing a feature into three artifacts causes scattering. Thus, requiring developers to maintain the consistency of multiple representations at once.
Pronounced learning curve – Knowledge on multiple technologies becomes the norm. Developers using MVC need to be skilled in multiple technologies.
We learned above that MVC is not a programming language. It is an architectural pattern that will help us make our source code organized and well-designed. The next question I would like to throw, is it really necessary to apply on all of your projects?