Analysis on MVC Programming Thought in PHP Programming

  • 2021-07-10 19:01:49
  • OfStack

At present, MVC programming idea of PHP has been widely used in the development of various large-scale projects, and many mature MVC frameworks are gradually known and widely used in various projects, such as ThinkPHP, codeigniter, Symfony, yii, cakePHP and so on. In this paper, the MVC programming idea of php under 1 is briefly described.

1. What is MVC

Simply put, it is to classify and layer the source code of the website.
The meaning of MVC 3 letters:
M: Model model, responsible for database operations.
V: View view, responsible for calling Model to retrieve data, and then calling templates to show the final effect.
C: The Controller controller, the entry to the program, decides which View to call instead and tells View what to do.
Thus, the program is executed in the order of C-V-M or C-M, the opposite of the name of MVC.

2. Why MVC

1. It can make the physical structure of website program more reasonable.

When building a website with PHP, the dumbest way is to build each page into an PHP file. If your website only has index. php, menu. php. article. php 3 pages, then you can not use MVC, but when we do a general website, it is obviously not acceptable for us to put all the pages in the root directory, so you need a reasonable idea to classify your code, divide them into different directories according to functions, and load and call by program intelligence, which is what MVC wants to help you do.

2. Make the code easier to maintain.

Let's look at a single page again. The stupidest way is to mix PHP code with HTML code, which is obviously not good enough. You have to distinguish between PHP and HTML when maintaining a website, which is a disaster for a programmer. So many people use Smarty, which can separate "data processing" from "page presentation". This is really good, and many people are doing this, but this is not MVC. What MVC needs to do is to subdivide "data processing" into "logical processing" and "database operation", which is called layering.
So when your program errors or want to modify, it becomes very easy, when the page shows errors, you go to check V or template files; When there is something wrong with logic, you should check C and V;; Check M when your database operation is wrong.
In fact, MVC1 divides one page of PHP into four pages, namely C, V, M and template. Perform their duties and facilitate management.

3. Facilitate code reuse.

MVC will put one big function in one directory, that is, it will be managed by one C.
For example, if we want to make a website with a member system, we can put all the codes related to members into the user directory, which is managed by User_Controller system 1. When our other website also needs a member system, we can directly copy this directory and modify the interface 1.

3. The idea of PHP to realize MVC

We need three base classes: Controller, View and Model, and then different C, V and M inherit them respectively and have corresponding attributes and methods. If you don't understand here, you can read the object-oriented book.

Here, we provide you with a design idea of MVC base class for reference only:

1. Design of Controller class

1 main () method, for the program to call, mainly through get and post variables to determine how to deal with.
1 getModel ($model) method that calls the M of the corresponding directory when the database needs to be called.
1 display ($view) method, which is called in the main () method, loads the corresponding V, and drops the main () method corresponding to V;

2. The design of the View class is very similar to that of Controller

1 main () method, which is called when C loads V to enable the program to continue execution.
1 getModel ($model) method that calls the M of the corresponding directory when the database needs to be called.
1 display ($template), invokes the corresponding template file, and passes the data to the template.

3. Design of Model class

You can define 1 properties, such as which tables to operate on, which fields to operate on, and so on.
1 getDB () method to get an instance of a database class (database class 1 is generally designed with a singleton pattern)
1 load () method that loads 1 data.
An add () method that automatically constructs SQL statements based on defined properties and performs insert operations.
1 eidt () method, same as above, but performs modification operation.
1 del () method, same as above, but performs delete operation.
In order to enable novices to better understand how my idea works, we now simulate a user login scenario to see how MVC works.
Now assume that all the data is submitted to index. php,

Step 1:
We submit each get variable and tell index. php which C to use, for example, index. php? controller = user
Then index receives the get variable and doesn't need to do anything. It directly finds/user/controller. php and throws all the data to him. GET and POST are global, so index. php doesn't need to do anything, just call the main function of C directly, and the task of index. php is completed.

Step 2:
The main function of C starts executing, Check the variables, Find the login operation to be performed by the user (it is very simple, you can use post variables do=login), Then call getModel, load the corresponding M class (for example,/user/models/model. php), and instantiate, call the load method of the instance, load the user's data, judge whether it is related to the password 1 submitted by the user, if the submitted data is incorrect, header jumps to the error page, if it is correct, call display () method, load the corresponding V (for example,/user/views/details. php), and instantiate, call its main () function, and enter step 3. 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 and rewrite the retrieved data, or pass the parameters (such as SESSION) when C instantiates V. When V has determined that the data is obtained, display () loads the template and MVC is finished.
Of course, due to the limitation of words and energy, what is written here is only a very brief summary, and many details should be considered in actual implementation. However, when I designed MVC, my general idea was like this, and I also used it in practice, and I felt good.


Related articles: