thinkphp Multi-layer MVC usage Analysis _php Example

Source: Internet
Author: User

The examples in this article describe the thinkphp multi-tier MVC usage. Share to everyone for your reference, specific as follows:

Thinkphp supports multi-tier design.

1. Model Layer Models

Use multi-tier directory structure and naming conventions to design multilayer models, such as in project design if you need to distinguish between the data layer, the logical layer, the service layer and so on different model layer can create model,logic,service directory under the module directory, the user table of all model operations into 3 layers.

1.model/usermodel is used to define automatic validation of data correlation, automatic completion and data access interface

2.logic/userlogical used to define user-related business logic

3.service/userservice 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{}

Logical 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{}

Can be called using the built-in D method or the M method when called

D (' user ')//instantiation of Usermodel
D (' user ', ' Logic ')//materialized userlogic
D (' user ', ' Service ')//instantiation UserService

There is no second parameter model file name when calling the default model layer data access interface class, the default model layer is mode, or you can change the settings as follows:

Copy Code code as follows:
' Default_m_layer ' => ' Logic ',//change the default model-layer name to Logic

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

D (' user ')//instantiate userlogic
d (' user ', ' Model ')//instantiate Usermodel
d (' user ', ' Service ')//instantiation UserService

You can see the use of D (' User ') by default will instantiate the Userlogice class, this is very flexible, if we data validation, automatic completion is completed in JS, and the data is from the service interface to complete, so completely can be as long as a service layer, The rest of the layer is not needed.

2. View Layer views

The view layer consists of a template and a template engine, and a common third-party template is. TPL, which allows you to use PHP code directly in the template, which can be easily differentiated using a table of contents (template topics), such as:

View/default/user/add.html
View/blue/user/add.html

A complex, multi-tiered view can also use different view catalogs to differentiate, for example:

View Normal View Layer directory
Mobile phone-side Access View Layer Directory

You can use different page styles for different templates, as well as the default view directory, as follows:

Copy Code code as follows:
' Default_v_layer ' => ' mobile ',///default view layer name to mobile

3. Controller Layer Controller

There are two types of thinkphp controllers, one is the core controller, the other is the business controller, the core controller is in the thinkphp directory, for example Thinkphp\thinkphp\library\think\controller\ HproseController.class.php, responsible for the application of the scheduling control, including HTTP request interception, forwarding, loading configuration and so on. What we are going to discuss here is the business controller, which is done by the user-defined controller class, and the implementation principle of the multi-tier business controller is similar to that of the model, such as the business controller and the event controller,

Controller/usercontroller//For user's business logic control and dispatch
Event/userevent//For user's event response operations

Event this has not been used, looks very high, the user events in the Web development is very small, most of the completion in JS.

The access controller home/controller/usercontroller.class.php is defined as follows:

namespace Home\controller;
Use Think\controller;
Class Usercontroller extends controller{
}

The event controller home/event/userevent.class.php is defined as follows:

namespace Home\event;
Use Think\controller;
Class Userevent extends controller{
}

Usercontrlller is responsible for external interaction responses, requesting responses through URLs, such as http://serverName/User/index,UserEvent responsible for internal event responses and only internal calls to a (' User ', ' event '); Also, we can set the default controller layer:

Copy Code code as follows:
' Default_c_layer ' => ' event ',///default controller layer name changed to event

internal and external is isolated, multi-layer controller is not mandatory, can be based on the needs of the application of the free layering, the controller can be called according to the needs of different hierarchical models, can also display a different hierarchical view to achieve different themes.

In the three layer of MVC, thinkphp does not rely on M and V, can only C or only V, the user only needs to define the view, in the absence of C can also automatically identify, but this kind of weird writing will make a lot of novice programmers are very confused.

Multi-tier design is not used in the current project, see a lot of in. NET project, next time use to supplement.

More interested in thinkphp related content readers can view the site topics: "thinkphp Introductory Course" and "thinkphp Common methods Summary"

I hope this article will help you with the PHP program design based on 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.