This article mainly introduces the MVC programming idea in PHP programming, which is helpful for a deep understanding of MVC programming ideas, if you need it, you can refer to the MVC programming idea of PHP, which has been widely used in the development of various large projects, many mature MVC frameworks are gradually well known and widely used in various projects, such as ThinkPHP, codeigniter, Symfony, yii, and cakePHP. This article briefly introduces the MVC programming idea of php.
1. what is MVC?
Simply put, the website source code is classified and layered.
The meanings of the three MVC letters:
M: Model, responsible for database operations.
V: View, which calls the Model to retrieve data and then calls the template to display the final effect.
C: the Controller, the program entry, decides which View to call and tells the View what to do.
In this case, the execution order of the program is C-V-M or C-M, and the MVC name is exactly the opposite.
II. why MVC?
1. make the physical structure of website programs more reasonable.
When using PHP to build a website, you may build a PHP file for each page in the most stupid way. If your website only has index. php, menu. php. article. three php pages, you don't need MVC, but when we do a general website, we usually have dozens of pages, and it is obviously not acceptable to put all the pages in the root directory, so you need a reasonable idea to classify your code, divide them into different directories by function, and load and call the code intelligently. this is what MVC will help you do.
2. make the code easier to maintain.
Let's take a look at a single page. the most stupid way is to mix PHP code with HTML code. this is obviously not good enough. you have to distinguish between PHP and HTML when maintaining your website, this is a disaster for a programmer. As a result, many people use Smarty to separate "data processing" from "page display". This is indeed good, and many people are doing this, but it is not MVC, MVC divides data processing into logical processing and database operations.
In this way, when your program is wrong or you want to modify it, it becomes easy. When an error is displayed on the page, you can check the V or template file; when there is a logic problem, you should check C and V; when your database operation is wrong, you should check M.
In fact, MVC generally divides a PHP page into four pages: C, V, M, and template. Perform their respective duties to facilitate management.
3. facilitates code reuse.
MVC usually puts a large function under a directory, that is, it is managed by a C.
For example, to create a website with a membership system, we can put all the code related to members in the user directory, which is managed by User_Controller in a unified manner. when another website also needs a membership system, we can directly copy the directory and modify the interface.
III. idea of implementing MVC in PHP
We need three base classes: Controller, View, and Model. different C, V, and M inherit from each other and they have corresponding attributes and methods. if you do not understand them here, you can read the object-oriented book.
Here we provide you with a design idea for the MVC base class for your reference only:
1.Controller class design
A main () method is called by the program. The get and post variables are used to determine how to handle the problem.
A getModel ($ model) method calls the M of the corresponding directory when you need to call the database.
A display ($ view) method, called in the main () method, loads the corresponding V, and drops the main () method of V;
2.The design of the View class is similar to that of the Controller.
A main () method. this method is called when C loads V so that the program can continue to run.
A getModel ($ model) method calls the M of the corresponding directory when you need to call the database.
A display ($ template) that calls the corresponding template file and passes the data to the template.
3.Model class design
You can define some attributes, such as tables to be operated and fields to be operated.
A getDB () method to obtain an instance of a database class (database classes are generally designed in single-piece mode)
Load () method to load a data.
An add () method automatically constructs an SQL statement based on the defined attributes and performs the insert operation.
An eidt () method, same as above, but is modified.
One del () method, the same as above, but the delete operation is performed.
To help new users better understand the working principles of my thinking, we now simulate a user login scenario to see how MVC works.
Now assume that all data is submitted to index. php,
Step 1:
We submit various get variables to tell index. php which C should be used, for example, index. php? Controller = user
Then index receives the get variable and does not need to do anything. you can directly find/user/controller. php, throwing all the data to him. Originally, GET and POST are global, so index. php does not need to do anything. you can directly call the main function of C. php task completed.
Step 2:
C's main function starts execution, checks the variables, and finds the login operation to be performed by the user (very simple, you can post the variable do = login), so 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, determine whether the password is consistent with the user submitted, if the submitted data is incorrect header jump to the error page, if it is correct, call the display () method to load the corresponding V (for example,/user/views/details. php), and instantiate it, call its main () function, and enter the third step. The task C has been completed, and the second operation is not performed in the main function.
Step 3:
You can choose to call getModel () to load M, rewrite and retrieve data, or pass the parameter (for example, SESSION) when C instantiates V. After V has determined the data, display (), load the template, and MVC execution is complete.
Of course, due to the limited words and energy, the content here is a very brief summary. many details should be taken into account during actual implementation. However, when designing MVC, the general idea is like this, in practice, I feel good.