ThinkPHP3.1 New feature: multi-layer MVC support

Source: Internet
Author: User
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:
  1. D ('user') // instantiate UserModel
  2. D ('user', 'logic ') // instantiate UserLogic
  3. 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:
  1. Tpl/default/User/add.html
  2. 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:
  1. Action/UserAction // used for the user's business logic control and scheduling
  2. 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.
  1. 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:
  1. 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.

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.