PHP's MVC programming idea has been widely used in the development of various large-scale projects. Many mature MVC frameworks have gradually become well-known and widely used in various projects. The more common ones are ThinkPHP, codeigniter, Symfony, yii, cakePHP, etc. This article briefly describes the MVC programming ideas of php.
1. What is MVC
Simply put, it is to classify and layer the website source code.
The meaning of the three letters of MVC:
M: Model model, responsible for database operations.
V: View, responsible for calling Model to retrieve data, and then calling templates to show the final effect.
C: Controller controller, the entry of the program, decides which View to call, and tells the View what to do.
In this way, the execution order of the program is C-V-M or C-M, which is exactly the opposite of the name of MVC.
2. Why MVC
1.It can make the physical structure of the website program more reasonable.
When building a website with PHP, the stupidest way is to build each page into a PHP file. If your website only has three pages, then you don’t need MVC. However, when we build general websites, there are dozens of pages, and putting all pages in the root directory is obviously not acceptable. So you need a reasonable idea to classify your code, divide them into different directories according to functions, and call them intelligently by the program. This is what MVC wants to help you do.
2.Makes the code easier to maintain.
Let's look at a single page. The stupidest way is to mix PHP code with HTML code. This is obviously not good enough. When you maintain a website, you have to distinguish where PHP and where HTML are. This is simply a disaster for a programmer. So many people use Smarty so that they can separate "data processing" and "page display". This is indeed good, and many people are doing this, but this is not MVC. What MVC needs to do is to divide "data processing" into "logical processing" and "database operations", which is what is called hierarchy.
In this way, when your program is wrong or wants to modify it, it becomes very easy. When the page displays an error, you check V or template files; when there is a problem with the logic, you check C and V; when your database operation is wrong, you check M.
In fact, MVC generally divides a page of PHP into 4 pages, namely C, V, M, and templates. Each performs its duties to facilitate management.
3.Favorable for code reuse.
MVC will usually put a large function in a directory, which is managed by a C.
For example, to build a website with a member system, we can put all the member-related codes in the user directory and be managed by User_Controller. When another website also needs a member system, we can copy this directory directly and modify the interface.
3. PHP's idea of implementing MVC
We need three base classes: Controller, View, Model, and then different C, V, and M inherit them separately and have corresponding properties and methods. If you don't understand it here, you can go and read the object-oriented book.
Here we provide you with a design idea of the MVC base class for reference only:
1. Controller class design
A main() method is called by the program, and it mainly determines how to deal with it through get and post variables.
A getModel($model) method calls M in the corresponding directory when it is necessary to call the database.
A display($view) method is called in the main() method, loads the corresponding V, and drops the main() method of the corresponding V;
2.The design of the View class is very similar to that of the Controller
A main() method is called when C loads V so that the program can continue to execute.
A getModel($model) method calls M in the corresponding directory when it is necessary to call the database.
A display($template), calls the corresponding template file, and passes the data to the template.
3.Model class design
You can define some properties, such as those tables to operate, those fields to operate, etc.
A getDB() method to obtain an instance of a database class (database classes are generally designed in single-piece pattern)
A load() method, loading a data.
An add() method can automatically construct SQL statements based on defined properties and perform insert operations.
An eidt() method, the same as above, but performs the modification operation.
A del() method, the same as above, but performs the delete operation.
In order to enable newbies to better understand how this idea works, we now simulate a user login scenario to see how MVC works.
Now assume that all the data is submitted to
first step:
We submit each get variable and tell which C to use. For example, this is possible? controller=user
Then the index receives the get variable and does nothing. It just finds /user/ and throws all the data to it. Originally, GET and POST are global, so there is no need to do anything. Just call the main function of C directly, and the task here is completed.
Step 2:
The main function of C starts to execute, checks the variables, and finds the login operation the user wants to perform (very simple, just post the variable do=login), so call getModel, load the corresponding M class (such as /user/models/), and instantiate it, call the load method of the instance, load the user's data, and determine whether it is consistent with the password submitted by the user. If the submitted data is incorrect, the header jumps to the error page. If it is correct, call the display() method, load the corresponding V (such as /user/views/), and instantiate it, call its main() function, and enter the third step. The task to this C has been completed, and the second non-operation is performed in the main function.
Step 3:
You can choose to call getModel() to load M, override the data, or pass the parameters over when C instantiates V (such as SESSION). After V has been confirmed to get the data, display(), load the template, and MVC is executed.
Of course, due to word count and energy limitations, what I wrote here is just a very brief summary. Many details need to be considered when implementing it, but when I designed MVC, I roughly thought it was like this, and I also used it in practice, and I felt good.