These days, every self-respecting web developer should know the acronym MVC. Some acronyms that Let’s see what these acronyms are and why they are so important.
- What is MVC?
- What is model-view-controller for?
- The three parts of MVC
- What is the MVC model
- MVC Views
- The controllers or controllers
- MVC Example
What is MVC?
MVC stands for Model-View-Controller, a software architecture pattern that separates web application development into three parts: interface, information access, and event handling.
What does it mean that it is a pattern? Well, let’s say that it is a development standard so that all of us who develop the web understand each other, following a common logic. Following this pattern we will have some guidelines to know quickly where each part of our code goes.
What is model-view-controller for?
Following the development through the MVC pattern gives us great advantages, including avoiding reinventing the wheel in each new project, and having clear guidelines on how and where we should program each part of the operation of our website.
The advantages are many, but among them stand out the following:
- MVC is a clear way of developing, with instructions that are easy to understand and in which a beginner can start very quickly.
- It simplifies the planning of solutions and adapts to the different possibilities of our developments.
- It allows a better maintenance of the developments. Being able to pass from one programmer to another, who knows this pattern, without adaptation problems.
- It improves the treatment of security and the solution of errors by having clear data inputs and outputs.
The three parts of MVC
What is the MVC model
The model is all that code of our application that interacts with the database or that deals with the persistence of the information. Reading, inserting, updating, and deleting data are common model tasks.
These code files are usually made up of OOP (Object Oriented Programming) Classes, but they can also be functions or sequential code (very rare in most frameworks). In addition, it is common to have them organized in a specific folder (models or models) and in most of frameworks php will also have in its name the word model. Example: userModel.php
This part of the code would enter into the Backend development.
Views are files that contain the code intended to produce a graphical output of the content. They are the part that is in charge of assembling the data for its visualization in the browser. Therefore, most of the content of these files will be HTML tags.
Views typically use content from models to display dynamic pages. In these files we must strictly adhere to printing data or traversing data structures such as arrays, being able to use loops or conditionals.
Views often use template engine languages, specific languages developed to give more clarity to the combination of data coming from the backend of the application (usually php). These languages also force the developer to strictly maintain the correct separation of the parts of the application stipulated in MVC. An example of a template engine is twig.
Just as models are typically stored in a named folder, views are typically stored in a directory named views either views.
MVC views are part of web development front end.
The controllers or controllers
As its name indicates, the controller/s is the web part where the code that receives and processes the client’s requests should go. Depending on the user’s actions on the web, the controller will request one or the other information from the model and display one view or another.
The controllers are usually located in a Controllers or Controllers folder, it is also common for the file names to have the word Controller and that your code is developed using OOP Classes (Object Oriented Programming).
The MVC controller is part of the back-end development. Its language is entirely server language, PHP.
With the following image you will surely see everything much clearer. An MVC schema where you can follow the order of interaction between the parts of the pattern.