This example describes the thinkphp multilayer MVC usage. Share to everyone for your reference, as follows:
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 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{}
The built-in D method or M method can be used to invoke the
D (' user ')//instantiation of Usermodeld (' user ', ' Logic ')//instantiation of USERLOGICD (' user ', ' Service ')//instantiation 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:
Copy the 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 ')//instantiation of USERLOGICD (' user ', ' Model ')//instantiation of Usermodeld (' user ', ' Service ')//instantiation 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.html
View/blue/user/add.html
A more complex multi-layered view can also be distinguished using different view catalogs, such as:
View Normal view Layer Catalog
Mobile phone-side access to the view-level directory
So different templates can use different page styles, you can also default view directory, as follows:
Copy the code as follows: ' Default_v_layer ' = ' mobile ',//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,
Controller/usercontroller//For user's business logic control and scheduling event/userevent//For user event response actions
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 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 the external interaction response, through the URL request response, such as http://serverName/User/index,UserEvent responsible for internal event response and only internally call a (' User ', ' Event '); Again we can set the default controller layer:
Copy the 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 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.
More interested in thinkphp related content readers can view this site topic: "thinkphp Introductory Course" and "thinkphp Common methods Summary"
It is hoped that this article is helpful to the PHP program design based on thinkphp framework.