The so-called 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 idea to classify your code, divide them into different directories by function, and load calls by program intelligence, which is what MVC will help you with. 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 Requires three base classes: Controller, View, Model, and 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. To provide you with an MVC base class design ideas, for reference only: 1. The controller class is designed with a main () method that can be called by the program, mainly by the get and post variables to determine what to do with it. 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 design of the 2.View class is very similar to the controller. A main () method that calls this method when C is loaded into 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. The 3.Model class is designed to define properties such as manipulating those tables, manipulating those fields, and so on. A Getdb () method that obtains an instance of a database class, (the database class is generally designed with a single-piece schema) and 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, tell index.php which C, for example, can this index.php? Controller=user then index receives the get variable, and nothing needs to be done to find/user/directly controller.php, throw all the data to him, originally get and post is the global, so index.php also do not need to do, directly call C's main function can be, to this index.php task completed. The second step: 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 instantiate, call the instance of the 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. The third step: you can choose to call Getmodel () load m, rewrite the data, you can also instantiate V in C when the parameters passed (such as session), when V has been 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. |