The traditional procedural development method begins to show its insufficiency when dealing with medium-sized and above applications. Even if we can complete the requirements quickly, after the requirements change or when performing post-maintenance, we will fall deeply into the trap we built early. Therefore, using an object-oriented approach to implement the MVC pattern will provide us with a clear idea for sorting out the program's architecture.
What is MVC?
There are many definitions and explanations of MVC. We can find a more detailed explanation in Wiki or [2]. I do not intend or have the ability to explain it in depth here. From the perspective of PHP development, MVC can be summarized as:
View (The View):
When it comes to views, many of us think of template engines (such as Smarty, etc.). In fact, it is a variety of outputs, such as html templates and Javascript files.
Module (The Model)
Module represents the logic of the program and is usually called the business logic layer in enterprise applications. Generally speaking, the work done by this layer is to process the original data into meaningful data sequences stored according to the data structure we designed, and hand these data to the view for processing. Normally, a data abstract class is used in the module to perform processing related to data operations.
Model usually contains functions used to deal with the database.
The Controller
The Controller is the first stop for all WEB applications. It accepts received parameters, such as the $_GET variable, and then responds accordingly.
There are also many debates about whether MVC is suitable for PHP. People continue to discuss whether MVC is suitable for PHP[3]. Now there are many MVC frameworks, such as those listed in PHP MVC Frameworks[4] . So, why are people so keen on MVC, and why should we use MVC in our designs.
Why use MVC?
MVC was first used to solve desktop GUI programming problems. The earliest MVC framework should be Model 2 proposed by Sun in 1999, which later evolved into Struts. MVC gives people a deep impression, but when we use it, we don't seriously think about why we use MVC.
In traditional desktop applications, once something happens in the Model, we can actively refresh the View interface to show the changes that have occurred in the background. In web applications, we seem to be limited to the traditional Http Request/Response method, and we seem to have no way to let the client update. This discussion does not mean that MVC cannot be used to develop WEB applications, but that to some extent, it is not the most suitable.
There are still many debates about using MVC[1], but I believe that all people who are accustomed to using MVC to organize their own projects will definitely not give up MVC when asked to choose a new project architecture.