Home  >  Article  >  Backend Development  >  ThinkPHP3.1 new features multi-layer MVC support_PHP tutorial

ThinkPHP3.1 new features multi-layer MVC support_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:24:43700browse

1. Model layer: The default model layer is composed of the Model class. However, as the project grows and the business system becomes more complex, it is difficult for a single model layer to meet the requirements. From 3.1 Multi-layer Model support has been launched. The design idea is very simple. Different model layers still inherit from the system's Model class, but they are distinguished in the directory structure and naming convention. For example, in a certain project design, the data layer needs to be distinguished. , logic layer, service layer and other different model layers, we can create Model, Logic and Service directories under the Lib directory of the project, and divide all model operations on the user table into three layers:

Data layer: Model/UserModel is used to define data-related automatic verification and automatic completion and data access interfaces
Logic layer: Logic/UserLogic is used to define user-related business logic
Service layer: Service/UserService is used to define user-related service interfaces, etc.

These three model operation classes can all inherit the Model class, so that the operation of user data is very clear. When calling, you can also use the built-in D method to conveniently call:

D('User') //实例化UserModel
D('User','Logic') //实例化UserLogic
D('User','Service') //实例化UserService

The hierarchical division of model layers is very flexible, and developers can freely define hierarchies according to the needs of the project.

2. View layer: consists of a template and a template engine. PHP code can be used directly in the template. The design of the template engine will be described later. Other third parties can also be supported through the driver. template engine. Multiple layers of views can be easily distinguished by directories, for example:

Tpl/default/User/add.html
Tpl/blue/User/add.html

3. Controller layer: ThinkPHP’s controller layer consists of core controller and business controller. The core controller is completed by the App class inside the system and is responsible for applications (including modules and (operation) scheduling control, including HTTP request interception and forwarding, loading configuration, etc. The business controller is completed by the user-defined Action class. Version 3.1 has added support for multi-layer service controllers. The implementation principle is similar to the layering of the model, such as business controller and event controller:

Action/UserAction //用于用户的业务逻辑控制和调度
Event/UserEvent //用于用户的事件响应操作

UserAction is responsible for external interaction response and responds through URL request, such as http://serverName/User/index, while UserEvent is responsible for internal event response and can only be called internally

 A('User','Event');

So it is isolated from the outside world. The division of multi-layer controllers is not mandatory and can be layered freely according to the needs of the project. In the controller layer, you can call the layered model as needed, or you can call the view templates of different directories.
At the same time, the R method can also support the calling of multi-layer controllers, adding a third parameter to represent the layer name of the controller, for example:

R('User/register',array(15),'Event');

means calling the register method of the UserEvent controller and passing in parameter 15.
In the three layers of MVC, ThinkPHP does not rely on M or V, and can even have only C or only V. This is a very important user experience design in ThinkPHP design. Users only need to define Views can be automatically recognized without C.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/825375.htmlTechArticle1. Model layer: The default model layer consists of the Model class, but as the project grows and With the complexity of the business system, it is difficult for a single model layer to meet the requirements. Starting from 3.1, multiple...
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