ThinkPHP is based on the MVC (Model-View-Controller, Model-View-Controller) mode. However, it supports multi-Layer design. ThinkPHP is based on the MVC (Model-View-Controller, Model-View-Controller) mode. However, it supports multi-Layer design.
Model layer: The default Model layer is composed of Model classes. However, as the project grows and the business system becomes more complicated, a single Model layer is difficult 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 they are differentiated in the directory structure and naming rules, for example, in a project design, we need to differentiate different Model layers such as the data layer, Logic layer, and Service layer. we can create the Model, Logic, and Service directories under the Lib Directory of the project, 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.
The three Model operation classes can inherit the Model class in a unified way, so that the operation on user data is very clear. during the call, you can also use the built-in D method to conveniently call it:
- D ('user') // instantiate UserModel
- D ('user', 'logic ') // instantiate UserLogic
- D ('user', 'service') // instantiate UserService
The hierarchical division of the model layer is flexible for copying code. developers can freely define layers based on project requirements.
View layer: It consists of a template and a template engine. PHP code can be directly used in the template. the template engine design will be described later. other third-party template engines can be supported through drivers. The multi-layer view can be easily distinguished by Directories, for example:
- Tpl/default/User/add.html
- Tpl/blue/User/add.html
Copy code
Controller layer: The controller layer of ThinkPHP consists of the core controller and business controller. The core controller is composed of the App class in the system and is responsible for scheduling control of applications (including modules and operations, including HTTP request interception and forwarding, loading configuration, etc. The service controller is completed by the user-defined Action class. In analyticdb 3.1, multi-layer service controllers are supported. Its implementation principle is similar to that of the model, such as business controller and event controller:
- Action/UserAction // used for the user's business logic control and scheduling
- Event/UserEvent // used for user Event Response Operations
Copy the code UserAction to respond to external interactions and request responses through URLs, such as http: // serverName/User/index. UserEvent is responsible for internal event responses and can only be called internally.
- A ('user', 'event ');
The copied code is isolated from the outside. The division of multi-layer controllers is not mandatory, and can be freely divided according to the needs of the project. In the controller hierarchy, you can call the hierarchy model as needed, or call view templates of different directories.
At the same time, the R method can also support calling 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 the code to call the register method of the UserEvent controller and pass in parameter 15.
In the MVC layer 3, ThinkPHP does not depend on M or V, or even only C or V. This is an important user experience design in ThinkPHP design, you only need to define the view and can automatically identify it without C.