Home >Backend Development >PHP Tutorial >Let's talk about the implementation ideas of MVC in PHP_PHP tutorial

Let's talk about the implementation ideas of MVC in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:33:07810browse

I believe there are already many articles like this, but I am still willing to share my experience with you today. It is purely original and I have no reservations. I hope it will be helpful to newbies. If there is anything wrong, you are welcome to point it out.

What is MVC? To put it simply, it means classifying and layering the website source code.

The meaning of the three letters of MVC:

  • M: Model model, responsible for database operations.
  • V: View, responsible for calling the Model to retrieve data, and then calling the template to display the final effect.
  • C: Controller, the entry point 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.

Why MVC?

  1. Can make the physical structure of the website program more reasonable
  2. 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: index.php, menu.php.article.php, then you don’t need MVC. But when we make a general website, we often have dozens of pages. It is obviously not appropriate to put all the pages in the root directory. What we can accept, so you need a reasonable idea to classify your code, divide them into different directories according to functions, and intelligently load and call them by the program. This is what MVC will help you do.

  3. Make code easier to maintain
  4. Let’s look at a single page again. The stupidest way is to mix PHP code and HTML code. This is obviously not good enough. When maintaining the website, you have to distinguish where is PHP and where is HTML. This is very difficult for a programmer to do. Say, it's just a disaster. So many people use Smarty, so that they can separate "data processing" and "page display". This is really good, and many people are doing it, but this is not MVC. What MVC has to do is to separate "data processing" "is further divided into "logical processing" and "database operations", which is what is called layering.

    This way, when your program has an error or you want to modify it, it becomes very easy. When the page displays an error, you can check the V or template file; when there is a problem with the logic, you can check it. C and V; when your database operation error occurs, check M. In fact, MVC generally divides a PHP page into 4 pages, namely C, V, M, and template. Each performs its own duties to facilitate management.

  5. Conducive to code reuse
  6. MVC will generally put a large function in a directory, which is managed by a C.

    For example, if we want to build a website with a membership system, we can put all the membership-related codes in the user directory and manage them uniformly by User_Controller. When another website of ours also needs a membership system, we can directly put Copy this directory and modify the interface.

The idea of ​​implementing MVC in PHP

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

I will provide you with a design idea of ​​MVC base class for reference only:

Design of Controller class

A main() method for the program to call, mainly deciding how to process it through get and post variables.

A getModel($model) method calls M in the corresponding directory when the database needs to be called.

A display($view) method is called in the main() method, loads the corresponding V, and replaces the main() method of the corresponding V.

The design of View class is very similar to Controller

A main() method, this 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 the database needs to be called.

A display($template) calls the corresponding template file and passes the data to the template.

Design of Model class

You can define some attributes, such as which tables to operate, which fields to operate, etc.

A getDB() method to obtain an instance of a database class (database classes are generally designed using singleton mode)

A load() method to load a data.

An add() method can automatically construct SQL statements based on defined attributes and perform insertion operations.

An eidt() method, the same as above, but performs modification operations.

A del() method, same as above, but performs deletion operation.

In order to enable novices to better understand the working principle of my idea, we now simulate a user login scenario to see how MVC works.

Now assume that all data is submitted to index.php.

Step one:

We submit each get variable to tell index.php which C to use, for example, index.php?controller=user.

Then index receives the get variable, and there is no need to do anything. It directly finds /user/controller.php and throws all the data to it. Originally GET and POST are global, so index.php does not 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 to execute, checks the variables, and finds the login operation that the user wants to perform (very simple, you just post the variable do=login), then calls getModel and loads the corresponding M class (such as /user /models/model.php), 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 will jump to the error page. If it is correct, , call the display() method, load the corresponding V (such as /user/views/details.php), instantiate it, call its main() function, and enter the third step. At this point, the task of C has been completed, and the second operation is performed in the main function.

Step 3:

You can choose to call getModel() to load M and rewrite the retrieved data, or you can pass the parameters (such as SESSION) when C instantiates V. After V has determined to get the data, display() , load the template, and MVC is executed.

Of course, due to word count and energy limitations, what is written here is only a very brief summary. There are many details to consider during actual implementation. But when I designed MVC, this was the general idea, and I have used it in practice, and I feel good about it.

There are several online frameworks and popular online systems, such as phpcms V9, wiki, and some group buying programs. They all feature an entry file, including a model, a controller, and a view. The specific boundaries are also unclear. It's all about feel. However, in order to better understand myself, MVC is actually not easy to understand at the beginning. I want to conceive my own framework. In fact, the principle should still be them.

  1. Part One: Bootstrap. That is, the entry file of the framework. Generally it is index.php in the root directory, its function is similar to BIOS.
  2. Part 2: Data initializer. The function of this part is to initialize and safely process the data returned by the client. In order to facilitate the operation of the framework and facilitate the acquisition of data, the data may be converted into the format you want.
  3. Part 3: Control loader. There is a controller in MVC. The function of this part is to import and load the controller based on the initialization data.
  4. The fourth part is: the controller. This part of the content is the most critical and the most free place. It loads the model, calls the updated cache, processes business logic, loads the template engine for parsing, and returns the page.
  5. Part 5: Template engine. This is generally used at the end of the controller (the end of the business logic, not necessarily the end of the program). The quality of the template engine is related to the parsing speed of the template and the flexibility of template production.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/752532.htmlTechArticleI believe there are already many such articles, but I am still willing to share my experience with you today. It's purely original and I have no reservations. I hope it will be helpful to newbies...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn