What is MVC?
Simple to say is the site source code classification, layering.
MVC three-letter meaning:
M:model model, responsible for database operations.
The V:view view is responsible for invoking the model data and then invoking the template to show the final effect.
The C:controller controller, the entry of the program, decides which view to call, and tells the view what to do.
In this case, the order of execution of the program is C-V-M or C-M, and the name of MVC is the opposite.
Why MVC?
1. Can make the website program physical structure more reasonable.
When using PHP to build a Web site, the most stupid way, you may put each page to build a PHP file. If your site only index.php,menu.php.article.php three pages, then you can not use MVC, but we do the general site, often dozens of pages, put all the pages in the root directory is obviously not what we can accept, so you need a reasonable thought to Your code classification, by function to separate them into different directories, and by the application of intelligent loading calls, this is what MVC will help you do.
2. Make the code easier to maintain.
We look at a single page, the most stupid way, is the PHP code and HTML Code mix, which is obviously not good enough, you have to maintain the site when you have to distinguish between PHP, where is the HTML, which for a programmer, it is simply a disaster. So many people use smarty, so that can be "data processing" and "page display" separate, this is really good, there are many people are doing so, but this is not mvc,mvc to do is to "data processing" is divided into "logical processing" and "Database Operations", which is said to be layered.
So when your program is wrong or want to modify, it becomes very easy, when the page display error, you go to check the V or template file, when the logic is a problem, you go to check C and V, when your database operation error to check M.
In fact, MVC generally want to divide a page of PHP into 4 pages, respectively, c,v,m, template. Their respective responsibilities, convenient management.
3. Facilitates code reuse.
MVC will generally put a large function in a directory, that is, by a C to manage.
For example, to make a website with Membership system, we can put the member-related code into the user directory, by User_controller Unified management, when our other site also needs the membership system, we can directly copy the directory past, modify the interface can be.
The idea of implementing MVC in PHP
We need three base classes: Controller, View, Model, then different C, V, and M inherit their corresponding properties and methods, if you do not understand here, you can go to look at the object-oriented book.
I will provide you with an MVC base class design idea, for reference only:
1. Design of Controller class
A main () method, which is intended for program invocation, is primarily determined by the get and post variables.
A Getmodel ($model) method that calls the corresponding directory's m when the database needs to be called.
A display ($view) method, called in the Main () method, loads the corresponding V, and falls off the main () method of V;
The 2.View class is similar in design to controller
A main () method that calls this method when C loads V, so that the program can continue to execute.
A Getmodel ($model) method that calls the corresponding directory's m when the database needs to be called.
A display ($template) that invokes the corresponding template file and passes the data to the template.
Design of the 3.Model class
You can define properties such as how to manipulate those tables, manipulate those fields, and so on.
A Getdb () method to obtain an instance of a database class (the database class is generally designed in single-piece mode)
A load () method that loads a data.
An Add () method that automatically constructs an SQL statement based on a defined property and performs an insert operation.
A Eidt () method, as above, but performs a modification operation.
A del () method, ditto, but performs a delete operation.
To make it easier for novices to understand how this idea works, let's now simulate a user login scenario and see how MVC works.
Now suppose that all the data is submitted to index.php,
The first step:
We submit each get variable to tell index.php which C to use, for example, to Index.php?controller=user
Then index receives the get variable, does not need to do anything, directly find/user/controller.php, throw all the data to him, originally get and post is the global, So index.php does not need to do anything, directly call C's main function can be, to this index.php task completed.
Step Two:
C's main function starts execution, checks the variable, discovers the user to perform the login operation (very simple, you post the variable do=login to be possible), then calls Getmodel, loads the corresponding m class (for example/user/models/model.php), and instantiates, Call the instance's Load method, load the user's data, determine whether the user submitted a password consistent, if the submitted data is incorrect header jumps to the error page, if correct, call the display () method, load the corresponding V (for example,/user/views/ details.php), and instantiate, call its main () function and enter the third step. The task to this c is completed, and the second operation is done in the main function.
Step Three:
You can choose to call Getmodel () to load m, rewrite the data, or in C to instantiate V, pass the parameters (such as session), when V has determined to get the data, display (), loading the template, MVC execution is complete.
Of course, because of the number of words and energy limitations, here is only a very brief summary, the actual implementation of the time to consider a lot of details, but I design mvc, the approximate idea is this, also used in the actual, feel good.
Brief introduction to MVC thought and how PHP implements MVC