Multi-layer MVC usage analysis of thinkphp

Source: Internet
Author: User
This article mainly introduces the usage of multi-layer MVC in thinkphp, and analyzes the principle and related usage skills of multi-layer MVC in thinkPHP based on examples, if you need it, refer to the example in this article to describe the multi-layer MVC usage of thinkphp. We will share this with you for your reference. The details are as follows:

ThinkPHP supports multi-layer design.

1. Model layer

Multi-layer directory structure and naming rules are used to design multi-layer models. for example, in project design, if you need to distinguish between the data layer and the logic layer, different Model layers, such as the Service layer, can create the Model, Logic, and Service directories under the module directory, and divide all Model operations on the user table into three layers.

1. Model/UserModel is used to define automatic data-related verification, automatic completion, and data access interfaces.

2. Logic/UserLogical is used to define user-related business Logic.

3. Service/UserService is used for user-related Service interfaces.

All three models inherit the Model class, such as the data layer Home/Model/UserModel. class. php.

namespace Home\Model;use Think\Model;class UserModel extends Model{}

Logic layer Home/Logic/UserLogical. class. php

namespace Home\Logic;use Think\Model;class UserLogic extends Model{}

Service Layer Home/Service/UserService. class. php

namespace Home\Service;use Think\Model;class UserService extends Model{}

You can use the built-in D or M method for calling.

D ('user') // instantiate UserModelD ('user', 'logic ') // instantiate UserLogicD ('user', 'service') // instantiate UserService

When calling the data access interface class under the default Model layer Model, there is no second parameter Model file name. the default Model layer is Model. you can also change the settings as follows:

The code is as follows:

'Default _ m_lay' => 'logic ', // change the DEFAULT model layer name to Logic.


In this case, the instantiation method needs to be modified accordingly.

D ('user') // instantiate UserLogicD ('user', 'model') // instantiate UserModelD ('user', 'service') // instantiate UserService

We can see that the UserLogice class will be instantiated by default when D ('user') is used. this is flexible. if we verify data, automatic completion is completed in js, data retrieval is completed from the service interface, so that only one Service layer is required, and other layers are not required.

2. View

The view layer consists of templates and template engines. common third-party templates are. tpl can directly use php code in the template. you can use directories (Template themes) to differentiate multiple layers of the view. for example:

View/default/User/add.html
View/blue/User/add.html

For a complex multi-layer view, different View directories can be used to differentiate the view. for example:

View common view layer Directory
Mobile terminal access View layer Directory

In this way, different templates can use different page styles and default view directories, as shown below:

The code is as follows:

'Default _ v_lay' => 'mobile', // The DEFAULT view layer name is changed to Mobile.

3. Controller layer Controller

There are two types of ThinkPHP controllers: core Controller and Service Controller. The core Controller is in the ThinkPHP Directory, for example, thinkphp \ ThinkPHP \ Library \ Think \ Controller \ HproseController. class. php is responsible for application scheduling control, including Http request interception, forwarding, and loading configuration. Here we will discuss the business controller, which is implemented by the controller class defined by the user. the implementation principle of the multi-layer Business controller is similar to that of the model, such as the business controller and event controller,

Controller/UserController // used to control the user's business logic and schedule Event/UserEvent // used for user Event Response Operations

This event has never been used, and it seems very high. There are very few user events in web development, most of which are completed in js.

Access Controller Home/Controller/UserController. class. php is defined as follows:

namespace Home\Controller;use Think\Controller;class UserController extends Controller{}

The definition of the Event controller Home/Event/UserEvent. class. php is as follows:

namespace Home\Event;use Think\Controller;class UserEvent extends Controller{}

UserContrlller is responsible for external interactive responses and URL request responses, such as http: // serverName/User/index. UserEvent is responsible for internal event responses and can only internally call A ('user ', 'event'); similarly, we can set the default controller layer:

The code is as follows:

'Default _ c_lay' => 'event', // change the DEFAULT controller layer name to Event


The internal and external layers are isolated, and multi-layer controllers are not mandatory. they can be freely layered based on application requirements. different layered models can be called in the controllers as needed, you can also display different hierarchical views to implement different themes.

In the MVC layer 3, ThinkPHP does not depend on M and V. It can only be C or V. you only need to define the view and can automatically identify it without C, however, this strange writing method will confuse many beginners.

Multi-layer design has not been used in the current project, but many have been seen in the. net project. it will be used again next time.

I hope this article will help you design PHP programs based on the thinkPHP framework.

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.