SoFunction
Updated on 2025-03-10

Support for multi-layer MVC with new features of ThinkPHP3.1

1. Model layer:The default model layer is composed of Model classes, but with the increase of the project and the complexity of the business system, it is difficult for a single model layer to solve the requirements. Since 3.1, the support of multi-layer Model has been introduced. The design idea is very simple. Different model layers still inherit from the system Model class, but differentiated in directory structure and naming specifications. For example, in a project design, it is necessary to distinguish different model layers such as data layer, logic layer, service layer, etc. We can create Model, Logic and Service directories under the project's Lib directory, 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
Logical 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 be unified in inheriting the Model class, which makes the operation of user data very clear. When calling, you can also use the built-in D method to easily call:

D('User') //Instantiate UserModelD('User','Logic') //Instantiate UserLogicD('User','Service') //InstantiationUserService

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

2. View layer:It consists of templates and template engines. PHP code can be used directly in templates. The design of template engine will be described later. Drivers can also support other third-party template engines. Multiple layers of views can be simply distinguished by directories, for example:

Tpl/default/User/
Tpl/blue/User/

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

Action/UserAction //For user's business logic control and schedulingEvent/UserEvent //For user event response operations

UserAction is responsible for external interaction response, and responds through URL requests, 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. The division of multi-layer controllers is not mandatory, and they can be freely layered according to the needs of the project. In the controller hierarchy, you can call the hierarchy model as needed, or you can call the view templates of different directories.
At the same time, the R method can also support calls of multi-layer controllers, adding a third parameter to represent the layer name of the controller, for example:

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

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