Home  >  Article  >  Backend Development  >  Understanding the MVC pattern of Zend Framework_PHP tutorial

Understanding the MVC pattern of Zend Framework_PHP tutorial

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

What exactly is the Zend Framework MVC design pattern?

MVC (Model-View-Controller) pattern, that is, model-view-controller pattern, its core idea is to divide the entire program code into three components that are relatively independent but can work together. The specific functions are as follows:

  • Controller: Control layer. It plays the role of controlling the entire business process and realizes the collaborative work of the View layer and the Model layer.
  • Model: Data business logic layer. Implement specific business logic and status management functions.
  • View: presentation layer. It is a page that interacts with users, usually implementing data input and output functions.

In layman’s terms:

  • Controller - Programmers write the functions that a program should have (implement algorithms, etc.).
  • Model - Database experts perform data management and database design.
  • View - Interface designers design graphical interfaces.

To put it more simply:

  • Controller - serves as a directional guide. It does not perform any specific data operations. The database business logic is left to the Model to handle, and it can only be called in the controller. For example, if I want to query data in a table, only Function Show (parameters) is called in the Controller, and the specific Show (parameters) are implemented in the Model.
  • Model - Database experts conduct data management and database design, with specific Show (parameter) implementation methods.
  • View - Needless to say, it is what we call art design.

This MVC design pattern is a bit like the process when we go to a restaurant to eat in reality. When you go to a restaurant to eat, the process is like this. First, you have to order. No need to shout, a smooth and smooth little waiter will come in front of you and say, "Sir, what do you want to order?" , look at the menu, "Give me a whole small plate of fish-flavored shredded pork, and a bowl of rice." "Okay, sir, wait a minute." After saying that, the waiter trotted towards the kitchen, "What... tell me... Chef, don’t put ginger in it!" . . After a while, the waiter brought you the food you asked for, "Sir, please take your time~~ If you need any help, just call me." You wolfed down the food, and you didn't even know about the green peppers in it. Not even washed~~~

Just such a simple process of asking for food can be described using MVC. You are equivalent to View, the waiter is equivalent to Controller, and the damn chef who doesn’t wash the green peppers is Model. When you order food from the waiter, it is equivalent to our browser sending a request to the server. The waiter hands the dish you want to the chef, which is equivalent to the control layer handing the request sent by the browser to the business logic. It doesn’t care how the chef cooks this dish. Anyway, you can make me a plate of fish-flavored shredded pork. It’s his business whether to wash the green pepper or not. This is what we often hear from the old bird. When you hear "encapsulation", it's packaged well. You can even eat the fish-flavored shredded pork without washing the green pepper. Hehe~~ When the waiter brings a steaming plate of fish-flavored shredded pork in front of you. The process is equivalent to the business logic layer processing and sending the response back to the browser, and then displaying it to the user through the browser. This achieves a simple MVC coordination interaction, I don't know if you understand it. . .

Take a simple login module. The requirement is that you enter a username and password. If the input is the same as the predefined one, then you will enter the correct page. If it is different, an error message will be prompted "You YDon't fool me here, the input is wrong!"

In this small module, the initial page for entering username and password and the page displayed after verification are equivalent to View, and a Controller page is also needed here, which is used to receive the entered username and password. , and a Flg returned after verification (this Flg is used to judge whether your input is correct and jump to the corresponding page). Finally, there is still a Model missing, which is your class used for verification. , it processes whether what you input is different from the pre-ordered one, and then returns a Flg. In this way, the separation of logic and page is completely realized. My page doesn’t care what you do, I just display it anyway, and the Controller doesn’t care whether your Model is correct or not. Anyway, I gave you the username and password, and you have to give it. I will give you a Flg, just like the waiter doesn’t care whether the chef washes the green peppers or not. As for Medol, if you dare to give me a username and password, I will give you a Flg~~ If you see this You are enlightened, try writing about it later! !

When developing WEB applications now, one of the more popular methods is to use the "MVC" structure. Using this method to develop WEB applications is highly logical and simple, making program design more convenient and faster. What is "MVC"? To put it simply, it is a combination of "Model", "View" and "Controller", that is, all "three-layer" abstract structures. Of course, the "MVC" mentioned here " is for applications on the WEB, and "separating code and page design" is its leading idea. This idea is most vividly expressed in "Struts" using JavaServlet/JavaServerPages technology. Those who are interested can go to Http:/ /Jakarta.Apache.Org/Struts has a look. This design pattern allows programmers to focus on the design, writing and debugging of code, and web designers can have more time to invest in design without paying attention to specific function implementations. , this division of labor is completely suitable for large-scale projects or enterprise-level distributed application development.

From the launch of PHP5, we can see that the object-oriented functions are becoming more and more complete. It has become possible to use PHP to develop large-scale commercial websites or distributed enterprise applications. If combined with Zend Optimizer, code encapsulation has been achieved. sex.

How to use the "MVC" design pattern in PHP to develop WEB applications? Remember one thing (separate code and page design), and use a simple example to demonstrate it. For example, if you want to query member information from the database and display it on the web page, you need to consider two points here: 1. Connect to the database and retrieve the members. Information, 2. Display the member information on the web page, and connect to the database. We use a database class, call it the "DB" class. This class plays the role of "Model" at this time, and then we need to write A program that operates the "DB" class to retrieve data. The role played by this program is the "Controller". It accepts "POST" or "PUT" data from the client, and then calls the "DB" class to retrieve the data. Data, store these data in the "Controller", and finally pass the data to the "View" and display it according to a certain layout format. From the above analysis, we can see that the template plays the role here Of course, just a template class cannot be said to be MVC. The real MVC is not that simple. For details, please refer to "JSF".

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/752463.htmlTechArticleWhat exactly is the Zend Framework MVC design pattern? MVC (Model-View-Controller) pattern, that is, model-view-controller pattern, its core idea is to divide the entire program code into relatively independent and...
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