What is MVC and its working in PHP?
Consider you are on a website on your PC. There are three different parts of the MVC model one is the Model, controller and View. Each of these do a specific task.
The controller takes information from the website where the user is operating. The controller takes care of all the searches from the user.
The model up here takes all the connection to the database. Model knows everything about our database. The View and Controller are not connected to database. They are only used to show information and display the retrieved information.
The View mode is used to display what the user wants from the website. MVC creates a cycle of the entire process from the user wants to display the data he wish to and the display of the required data from the database. There is a requirement of controller for all the mode of MVC.
The following is the MVC model ,
I struggled a lot with this concept when I was new to programming. However, it's super easy and here's an easier way to remember it: -
Imagine a typical pharmaceutical shop and let's say there are three people handing it at any time -
1. Person 1: Talks to customers, reads their prescription. But doesn't know what to do next. This person simply conveys the information about client's need to Person 2.
2. Person 2: Knows which medicine is kept where in the shop, but is too lazy to go and fetch the medicines. This person simply orders person 3.
3. Person 3: Knows only one job - just to fetch the medicine that person 2 tells him to fetch, and hand over to person 1.
Now Person 1 is your "View" - with which users interact. Person 2 is the "Controller", responsible for determining what to do with the prescription and Person 3 is the "Model" who will actually fetch the medicine from the shelves (aka database).
MVC, or Model-View-Controller is a software architecture, or design pattern, that is used in software engineering, whose fundamental principle is based on the idea that the logic of an application should be separated from its presentation
The MVC principle is to separate the application into 3 main parts, known as the Model, the View, and the Controller.
A simple way to think of this would be to consider the following:
- A user interacts with the view - by clicking on a link or submitting a form.
- The Controller handles the user input, and transfers the information to the model
- The Model receives the information and updates it's state (adds data to a database)
- The View checks the state of the Model and responds accordingly (listing the newly entered data)
- The View waits for another interaction from the user.