Home > Backend Development > PHP Tutorial > Introduction to the multi-layer MVC support in the new features of ThinkPHP3.1

Introduction to the multi-layer MVC support in the new features of ThinkPHP3.1

不言
Release: 2023-04-02 09:16:02
Original
1725 people have browsed it

This article mainly introduces the support of multi-layer MVC in the new features of ThinkPHP3.1. It has a certain reference value. Now I share it with you. Friends in need can refer to it

Default The model layer consists of Model classes. However, as the project grows and the business system becomes more complex, it is difficult for a single model layer to meet the requirements. Multi-layer Model support has been launched since 3.1. This article mainly introduces the support of ThinkPHP3.1 multi-layer MVC. Friends who need it can refer to

1. Model (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. Multi-layer Model support has been launched since 3.1. The design idea is very simple. Different model layers still inherit from the system’s Model class. , but distinctions are made in the directory structure and naming conventions. For example, in a certain project design, it is necessary to distinguish between different model layers such as data layer, logic layer, and service layer. We can create Model, Logic, and Service under the Lib directory of the project. Directory, all model operations on user tables are divided 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
Copy after login

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: is composed 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, and it can also be supported through the driver. Other third-party template engines. Multiple layers of views can be simply distinguished by directories, for example:

Tpl/default/User/add.html
Tpl/blue/User/add.html
Copy after login

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

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

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

 A('User','Event');
Copy after login

, so it is isolated from the outside. 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. A third parameter is added to indicate the layer name of the controller, for example:

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

Indicates calling the register method of the UserEvent controller and passing in parameter 15.

In the three layers of MVC,
ThinkPHP does not depend on M or V, and can even only have C or only V. This is The design of ThinkPHP is a very important user experience design. Users only need to define the view, and it can be automatically recognized without C.

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

A more complete introduction to the new features of ThinkPHP3.1 that support Ajax

About thinkphp to implement password search for sending emails Introduction to the return function

The above is the detailed content of Introduction to the multi-layer MVC support in the new features of ThinkPHP3.1. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template