PEAR Practice: Realization of MVC Mechanism in PHP (1)

xiaoxiao2021-03-06  44

PEAR Practice: Realization of MVC Mechanism in PHP (1)

content:

1. MVC Overview 2. Implementation of the MVC Mechanism Reference About the author

related information:

1. Use PEAR to write your next PHP program 2, common module 3, easy to build your PEAR document 4 using phpdoc, create an intermediate database application layer

Nightsailer@hotmail.com) Beijing Saidi Data Co., Ltd. Engineers November 2001

In the past few articles, we discussed most of the content of PEAR. If you have to have a deep understanding of PEAR. So how do you apply in actual development? How to apply PEAR thinking to develop a project? From this article, I will lead you to the practice of PEAR. In the future, I will discuss some practical applications with everyone.

Ok, let's now use this release system as an example, discuss how, how to implement MVC in PHP? 1. MVC Overview 1. What is MVCMVC is an abbreviation for model_view_control. Model_view_control is a typical structure for software design. In this design structure, an application is divided into three parts: Model, View, and Controller, each part is responsible for different functions. Model refers to the application's data, and the operation of these data; View refers to the user interface; Controller is responsible for the synchronization between the user interface and the program data, that is, the operation of the two directions: First, in the user interface (view Operation completes the update of program data (Model), and the change is changed to the user interface in time to the user interface (VIEW). 1.2 MVC advantages

Make the program structure clearer, enhanced code stability In the MVC mechanism, the application is clearly divided into three parts of Model, View, Controller. These three parts respectively correspond to business logic and data, user interface, user request processing and data synchronization. We know that for business logic and data, user interface, user request processing, and data synchronization, the user interface changes the maximum possibility, and the control part is changed, while business logic is the most stable. So the division of this module function is conducive to selecting the focus during the code modification, rather than mixing code with different functions. Easiest development teams will divide the application into three parts of Model, View, Controller, as well as in accordance with the team members to divide in accordance with team members, which are conducive to three partial parallel development, speed up project progress. 2. Implementation of the MVC mechanism 2.1 Overview The first chapter introduces the general concept of MVC, this chapter will tell the specific implementation of the MVC mechanism in the project. 2.1.1 The implementation of the application technology MVC is closely related to the specific techniques of the application. In terms of website development, Java is more mature and typical. Taking an EJB project as an example, most systems may adopt HTML technology, and J2EE (Java 2, Enterprise Edition) related technologies, including: HTML, JSP, Javabean Enterprise Java Bean, servlet. These technologies are distributed in Model, View, Controller: The View section uses HTML, JSP, and JavaBean technology; the Controller section is used to servlet, Stateless Session bean, JavaBean technology; Model section may be used in Entity Bean, Stateless Session Bean JavaBean technology. However, we have used PHP technology in this project, so we need to make some compromise. On the one hand, try to make the logic implementation of the program to another technology implementation (such as J2EE), but at the same time, we must also take into account the limitations of this scripting language in PHP. PHP is not the same grade for the language of the object-oriented support with Java, even more than PERL. However, fortunately, our luck is not too bad, because PEAR inside PHP is an object-oriented extension. With PEAR specification, we can use some existing PEAR modules on the other hand, on the other hand, allowing us to achieve business logic in accordance with OO's idea. Therefore, we will discard the modular, process-proof programming method, transform your ideas, and package all things. Before you begin to introduce the actual design, we might want to see the demand of this project 2.1.2 The project demand is very simple, it is to be a website release system. The publishing system should be able to support channels, sub-channels, columns, division of privileges, user roles, customization of various templates of channels, process control of articles release ... More detailed, this is no longer described here. 2.1.3 Level Division Our applications can not only be divided into model, view, and controller, but also can be divided into page performance modules, business control modules, transaction management modules, tools, can be divided into page performance modules, business control modules, transaction management modules, tools, and tools. 4 modules of the module; or in accordance with the specific business division module. Let's introduce these two ways:

According to the business module, according to the actual business is divided into user management, article release, template maintenance, front desk, and system management five parts. Each part corresponds to one aspect of the distribution system. In this way, MODEL, View, and Controller implementation components are distributed among five business modules. According to the implementation of technical division, due to our implementation technology, HTML technology and PHP related technologies are used. According to these technologies, we divide the entire application into page performance layers, business control layers, transaction management, and tool modules. Such a 3-layer structure of MVC is refined to the above four logic layers (see the figure below) 2.1.4 Time-sequence diagram of a typical user request: If you still feel very empty and abstract about what I said now Let's take a look at a sequence diagram, this figure shows below the MVC, a request is how to respond and implement the entire process of operation from the data to the page display: The following will be divided into specific introduction to each module implementation method and The role of the entire application 2.2 The implementation of this part of the page is the most simple. The Template and the render class instantiated by the template. The template is written using pure HTML. There is no PHP code and control logic. It is precisely that this layer does not involve any business logic, its use is based on the selected template, and the data provided by the example, generates a complete HTML page The picture below shows a timing diagram of the performance layer: Simple telling, first is Asisstantobj, this is an instance of the Asisstant class (Abisstant, later we will discuss in detail), asistantobj In the init method, will analyze the performance layer 2 XML configuration files, template.xml, and render.xml, load the corresponding Template and Render information into the system and save it, can be used throughout the session. Secondly, by an example of the HANLDEROBJ, the HANLDER class, call the Asisstant class's LookupRender to find a template corresponding to the render instance calling the setTemplate and SetRenderData method, the former is loaded and initialized the corresponding template file, the latter is settings required. Instantiated actual data calls the render method of the Render instance. In the show method of the Render instance, it will call itself a renderit method (this method is a virtual method in the render class, each of which to inherit the render, the main job in this method is based on the basis RenderData generates an instance of the template, see the template mechanism below in the specific implementation), and then call the renderobj's Show method to display the instantiated page to the top of the screen. 2.2.1 Template mechanism We use the IT and ITX modules in PEAR as our template engine, because the ITX class is a pure PEAR class, which is similar to the template in phplib, but simple in use. . Moreover, ITX has some characteristics that are particularly suitable for this type of template. Considering that our page performance is not very special, use ITX to meet our needs. About the specific use details of ITX, you can refer to ITX source code, here is a simple method of use,

New IntegratedTemplateExtention ($ root = ") Build function, $ root is a directory for loading template files. Note that this may have a bug, when loading a template file using the following function, if the load is loaded with the path, I can't find that file, so I suggest you set the directory of the Template, then Use the following function to load the template. loadTemplatefile ($ filename, $ removeUnknownVariables = true, $ removeEmptyBlocks = true) template loaded from the specified file, $ removeUnknownVariables indicating whether to remove an unused identification variable, $ removeEmptyBlocks indicating whether to remove an unused block. SetRoot ($ root) Settings Templates Directory SetTemplate ($ TEMPLATE, $ REMOVEUNKNVARIABLES = true, $ removeemptyblocks = true) Load templates from the specified string. If you intend to store template data in the database (recommended), this function is useful. SetCurrentBlock ($ blockname) Sets the block name setvariable ($ VARIABLE, $ VALUE = "), set the value of the variable ID, parsecurrentblock () parses the current block TouchBlock ($ block) set the specified block, so that even if it is not used It is also displayed in the output ($ block = "__Global__", $ flag_recursion = false) Resolution Specifies the block getBlockList () Get the block of blocks in the template BlockXists ($ blockname) Decision Template whether there is a specified block 2.2.2 Due to the use of ITX, the definition of the template is simple, use the to define a HTML block that needs to instantiate, use {varName} to define The variables that need to be instantiated can be. For cycles, we don't need to perform on the page, but in the Render class control, we can generate a looped HTML code by selecting an HTML block, then instantiate the block repeatedly. Here is an example:

{Name} {agn} {sex}

$ OBJ-> SetCurrentBlock ('list');

For ($ I = 0; $ i <3; $ i ) {

$ Obj-> setvariable ('name', 'panfan');

$ Obj-> setvariable ('sex', 'boy');

$ OBJ-> Setvariable ('agn', $ i);

$ Obj-> ParsecurrentBlock ();

}

?>

This will generate the following HTML code: PANFAN 0 BOY Panfan 1 BOY PANFAN 2 BOY 2.2.3 Installation Templates As seen in the previous section, we will generate different pages by setting different template and different instantiated data by setting the Render class. So how do you connect the template and the Render it? This is a good problem. In order to simplify complex problems, we did not consider using a generic render that enables all templates and data, because this render logic is very responsible, but uses many render, each render can instantiate one or Several similar templates. Each template has an ID of its corresponding RENDER class, which is our pre-defined. We use an XML file to define all of the templates used in the app. This XML file name is called: Template.xml, its basic structure is like this: