Introduction to multi-layered MVC support in ThinkPHP3.1 new features

Source: Internet
Author: User
This article mainly introduces the ThinkPHP3.1 new features in the multi-layered MVC support, has a certain reference value, now share to everyone, the need for friends can refer to

The default model layer is composed of models, but with the increase of the project and complexity of the business system, a single model layer is difficult to solve the requirements, starting from 3.1 to launch the multi-layer model support. This article mainly introduces the support of ThinkPHP3.1 multi-layer MVC, the need for friends can refer to the following

1. Model layer: The default model layer is composed of models, but with the increase of the project and complexity of the business system, a single model layer is difficult to solve the requirements, Since 3.1, the support of multi-layer model has been introduced, the design idea is very simple, the different model layers still inherit from the models of the system, but make a distinction between the directory structure and the naming specification, for example, in a project design, it is necessary to distinguish different model layers, such as data layer, logic layer and service layer. We can create model, logic, and service directories under the project's Lib directory, dividing all the models of the user table into three tiers:

Data tier : Model/usermodel for defining data-related automatic validation and auto-completion and data access interfaces
Logic Layer : logic/userlogic to define user-related business logic
Service Tier : Service/userservice for defining user-related service interfaces, etc.

And these three model operation class unified inherit model class, so the operation of user data is very clear, in the call, you can also use the built-in D method is very convenient to call:

D (' user ')//instantiation of Usermodeld (' user ', ' Logic ')//instantiation of USERLOGICD (' user ', ' Service ')//instantiation UserService

Hierarchical partitioning of the model layer is flexible, and developers can freely define hierarchies based on the needs of the project.

2. View layer: consists of template and template engine, the template can be directly used in PHP code, template engine design will be described later, through the driver can also support other third-party template engine. Multiple layers of a view can be easily distinguished by directory, for example:

Tpl/default/user/add.htmltpl/blue/user/add.html

3. Controller layer: The controller layer of the thinkphp is composed of the core controller and the business controller, and the core controller is completed by the app class inside the system, which is responsible for the application (including module and operation) scheduling control, including HTTP request interception and forwarding, loading configuration, etc., the business controller is completed by user-defined action class. Version 3.1 began to increase support for multi-tier business controllers. Its implementation principles are similar to the layering of models, such as business controllers and event controllers:

Action/useraction//For user's business logic control and scheduling event/userevent//For user event response actions

Useraction is responsible for the external interaction response, through the URL request response, 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's isolated from the outside. The partitioning of multi-layer controllers is also not mandatory, and can be layered freely according to the needs of the project. In the controller hierarchy, you can call a layered model as needed, or you can call a different catalog view template.
At the same time, the R method can also support the call of a multilayer controller, adding a third parameter to represent the controller's layer name, for example:

R (' User/register ', Array (), ' Event ');

Represents the Register method that invokes the Userevent controller and passes in parameter 15.
In the MVC three layer,thinkphp does not rely on M or V, or even only C or only v, this is a very important user experience design in thinkphp design, users only need to define the view, in the absence of C can also be automatically recognized.

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.