Thinkphp Learning Note 7-Multilayer MVC

Source: Internet
Author: User

Original: thinkphp Learning note 7-Multilayer MVC

The thinkphp supports multilayer designs.

1. Model Layer Models

Multi-layered models are designed using multi-level directory structures and naming conventions, such as the need to differentiate between the data layer, the logical layer, the service layer, and other model tiers in the project design to create the Model,logic,service directory under the module directory and divide all the model operations on the user table into 3 layers.

1.model/usermodel for defining data-related automatic validation, auto-completion, and data access interfaces

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 extends model{}

Logic Layer home/logic/userlogical.class.php

namespace Home\logic;  Use Think\model; class extends model{}

Service Layer home/service/userservice.class.php

namespace Home\service;  Use Think\model; class extends model{}

The built-in D method or M method can be used to invoke the

// Instantiate Usermodel // Instantiate Userlogic // Instantiate UserService

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

' Default_m_layer '       =/  /  Change the default model layer name to logic

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

// Instantiate Userlogic // Instantiate Usermodel // Instantiate UserService

You can see that using D (' User ') by default will instantiate the Userlogice class, which is very flexible, if our data validation, auto-completion is done in JS, and the data is done from the service interface, so that can be as long as a service layer, The other layers are not needed.

2. View Layer View

The view layer consists of a template and a template engine, and a common third-party template is. TPL, which can be used directly in the template with PHP code, the multi-layered view can be easily used in the directory (template theme) to distinguish, for example:

view/default/user/add. HtmlView/blue/user/add.html

A more complex multi-layered view can also be distinguished using different view catalogs, such as:

View Normal view layer directory mobile phone side access to the views layer directory

So different templates can use different page styles, you can also default view directory, as follows:

' Default_v_layer '       =  //  default view layer name changed to mobile

3. Controller Layer Controllers

The thinkphp controller has two categories, one is the core controller, the other is the business controller, and the core controller is in the thinkphp directory, for example Thinkphp\thinkphp\library\think\controller\ HproseController.class.php, is 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's own defined controller class, and the implementation principle of the multilayer business controller is similar to that of the model, such as Business controller and event controller,

// business logic control and scheduling for users // Event response actions for the user

Events This has not been used, it looks very high, the web development of user events are very few, 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 extends controller{}

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

namespace Home\event;  Use Think\controller; class extends controller{}

Usercontrlller is responsible for the external interaction response, through the URL request response, such as http://serverName/User/index,UserEvent responsible for internal event response and can only be called internallyA(‘User‘,‘Event‘);同样我们可以设置默认的控制器层:

' Default_c_layer '       =  //  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 free layering, the controller can be called according to the needs of different hierarchical models, can also display different hierarchical views, to achieve different themes.

In the three layer of MVC, thinkphp does not rely on M and V, can only be C or V, users only need to define the view, and can be automatically recognized without C, but this kind of weird writing will make a lot of just getting started programmer very confused.

Multi-layered design is not yet available in the current project, and there is a lot to see in the. NET project.

Thinkphp Learning Note 7-Multilayer MVC

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: 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.