Speedphp learning tutorials-(understanding MVC) (original serialization 1)

Source: Internet
Author: User

Speedphp learning tutorials a total of eight articles, pictures of the article found not easy to get, see the original please move to Anhui PHP community, http://bbs.hfphp.org,

 

(This tutorial is not completely original. Some content and examples come from the official manual. Thanks to the simple and quick framework, we can learn it quickly)

From today on, the Anhui PHP community will provide you with a series of basic tutorials on speedphp combined with code instances to help you better understand the speedphp framework, better understanding of the popular MVC development ideas.

(If anything is incorrect, you are welcome to share your comments with us. However, you are also welcome to make better suggestions so that we can correct them in time ~)

Before learning the speedphp framework, we must understand some basic concepts and knowledge. I hope you can take a serious look, because these are the foundation, and we need to understand or even understand them ~

1. Understanding of MVC and template concepts
MVC originally exists in the desktop program, M is the exponential data model, V is the user interface, and C is the controller. The purpose of using MVC is to separate the implementation code of M and V, so that the same procedure can use different forms. For example, you can use a column chart or pie chart to represent a batch of statistical data. The purpose of C is to ensure synchronization between m and V. Once M changes, V should be updated synchronously.

Model-View-controller (MVC) is a software design model invented by Xerox PARC for programming language Smalltalk-80 in 1980s. It has been widely used. Sun's J2EE platform design model has been recommended in recent years and is welcomed by more and more developers who use ColdFusion and PHP. Model-View-controller mode is a useful toolbox, which has many advantages but also has some disadvantages.

2. How does MVC work?

Model-View-controller (MVC) separates the input, processing, and output processes of an application in the form of model, view, and controller, such an application is divided into three layers: model layer, view layer, and control layer.
A view represents a user interaction interface. For Web applications, it can be summarized as an HTML interface, but may be XHTML, XML, or applet. With the complexity and scale of applications, interface processing becomes challenging. An application may have many different views. The MVC design mode only applies to the collection and processing of View data and user requests, it does not include the processing of business processes in the view. The processing of business processes is handed over to the model. For example, an order view only accepts and displays data from the model to the user, and transmits input data and requests from the user interface to the control and model.
Model: Processes business processes/states and formulates business rules. The processing process of a business flow is black box for other layers. The model accepts the data requested by the view and returns the final processing result. The Design of business models is the core of MVC. Currently, the popular EJB model is a typical application example. It further divides the model from the perspective of application technology implementation to make full use of the existing components, but it cannot be used as the framework of the application design model. It only tells you that certain technical components can be used based on this model design, reducing technical difficulties. A developer can focus on the design of business models. The MVC design pattern tells us that the application model is extracted according to certain rules, and the extraction level is very important, which is also the basis for determining whether the developer is an excellent design. Abstraction cannot be separated from a specific object too far or too close. MVC does not provide a model design method. Instead, it only tells you to organize and manage these models to facilitate model reconstruction and improve reusability. We can use object programming for comparison. MVC defines a top-level class and tells its sub-classes that you can only do this, but you cannot limit your ability to do this. This is very important for programming developers.
Another important model of the business model is the data model. The data model mainly refers to the data storage (continuous) of object objects ). For example, save an order to the database and obtain the order from the database. We can list this model separately. All database operations are limited to this model.
The controller can be understood as receiving requests from users, matching the model with the view to complete user requests. The Division control layer is also very effective. It clearly tells you that it is a distributor, the model you choose, the view you choose, and the user requests you can complete. The control layer does not process any data. For example, when a user clicks a connection and the control layer accepts the request, it does not process the business information. It only transmits the user information to the model and tells the model what to do, select a view that meets the requirements and return it to the user. Therefore, a model may correspond to multiple views, and a view may correspond to multiple models.
The separation of models, views, and controllers allows a model to have multiple display views. If you change the model data through the controller of a view, all other views dependent on the data should reflect the changes. Therefore, the Controller will notify all views of the changes at any time, resulting in display updates. This is actually a model change-propagation mechanism. Relationship among models, views, and controllers and their main functions.

3. Advantages and Disadvantages of MVC:
Now that I have chosen a learning framework, I believe everyone should be familiar with it. If you do not know it, you can go to Baidu or Google. Everything is not perfect. As for his shortcomings, let's not talk about it. You can go and see it on your own ~

========================================================== ==============================================

This article describes the concept, design and practical application of MVC (Model-View-Controller) architecture in Network System (Web System) Development and in the SP framework.

First, let's look at the MVC Architecture:

Download(42.7 KB)

From the simple structure of MVC, we can solve it as follows:

The control layer (Controller layer) Interacts with visitors, and the controller is a set of similar interactions. Each interaction operation corresponds to an action ). In the SP framework, all controller classes must inherit from the spcontroller.
The model layer is used to access databases. In the SP framework, all model classes must inherit from the spmodel.
The view layer (view layer) is used to display the page template. In the SP framework, the view layer is integrated into the controller. Therefore, in actual development, only the page template is required for the view layer.

The above briefly describes the understanding of the MVC Architecture. The following describes the MVC concept in the SP framework, which may be abstract and hard to understand, but please read it with patience. After all, in actual development, it is necessary and important to understand the MVC concept.

The MVC concept in the SP framework divides the network system into three focuses: interaction, data, and page templates. That is, we design or analyze a network system, we can proceed from these three perspectives:

Interaction:
It is manifested in the functions provided by the system, the categories of these functions, and the operations of each category.
For example, in the PHP forum system, we can roughly divide it into several categories of functions, such as sections, posts, users, insite information, and background management, then, the user's functions include registration, logon, modification of user information, and so on.

For the control layer (Controller layer) of the SP framework, a function category can be called a controller class (if the overview scope of this type is large, in this case, you still need to split it into multiple controller classes. Each function of a single type is an action (action, method function of the controller class ). Then, based on each visitor's operation on the system, the SP framework automatically selects the corresponding action in the corresponding controller class for processing.

Data:
This is manifested in the operations performed by the system on the database. Almost all network systems need to deal with databases. Then, after analyzing the interaction and the system itself, we can roughly summarize the data required by the system. Then we sort and classify the data to determine which data tables are required by the system.
In the PHP forum just now, according to the "sections, posts, users, station information, and background management", we can basically summarize their requirements: plate record table, post record table, user table, station info table, background setting table, and so on.

For the model layer of the SP framework, a table corresponds to a model class. The model parent class (spmodel) in the SP framework provides most of these model classes with the data inventory function.

Page Template:
During the development of each network system, we need to create a considerable number of pages. Simply put, each action of the Controller may generate a page. Therefore, after clarifying all interactions and designing the controller (before writing code ), we can get a list of required page templates. With the help of the template engine, the page template can be developed simultaneously with the Controller to improve the development efficiency.
According to the above PHP Forum interaction design, we can obtain many page templates, such as the homepage template, partition template, and plate template; there are post content templates, post templates, and reply templates.

The SP framework integrates the view layer into the controller. Therefore, in development, the controller sends the required data to the template. The development required by the view layer is simply to create a Page Template according to the template syntax.

Based on the interaction, data, and page templates, we can fully analyze and design a network system. Note that these three concerns are not isolated, and "interaction" is the foundation of "data" and "Page Template, therefore, the analysis of "interaction" must be correct and complete. If you want to learn more about system analysis and design, go to the SP website and read the relevant documents.

After learning about the basic idea of speedphp MVC, we will introduce the basic Basics of the speedphp framework, so that you can continue to learn.

4. PNG(19.44 KB)

Downloads: 0

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.