Home >Backend Development >PHP Tutorial >thinkphp multi-layer MVC usage analysis, thinkphp multi-layer mvc_PHP tutorial

thinkphp multi-layer MVC usage analysis, thinkphp multi-layer mvc_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:02:09895browse

thinkphp multi-layer MVC usage analysis, thinkphp multi-layer mvc

This article describes the use of thinkphp multi-layer MVC with examples. Share it with everyone for your reference, the details are as follows:

ThinkPHP supports multi-layer design.

1. Model layer Model

Use multi-layer directory structures and naming conventions to design multi-layer models. For example, in project design, if you need to distinguish between different model layers such as data layer, logic layer, and service layer, you can create Model, Logic, and Service in the module directory. Directory, divides all model operations on user tables into three levels.

1.Model/UserModel is used to define data-related automatic 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

These three models all inherit the Model class, for example, 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 method or M method when calling

D('User') //实例化UserModel
D('User','Logic') //实例化UserLogic
D('User','Service') //实例化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:
Copy code The code is 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') //实例化UserLogic
D('User','Model') //实例化UserModel
D('User','Service') //实例化UserService

You can see that using D ('User') will instantiate the UserLogice class by default. This is very flexible. If we verify data, the automatic completion is done in js, and the data is obtained from the service It is completed in the interface, so that there is only one Service layer and other layers are not needed.

2. View layer View

The view layer is composed of templates and template engines. A common third-party template is .tpl. PHP code can be used directly in the template. Multiple layers of views can be distinguished simply by using directories (template themes), for example:

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

More complex multi-layer views can also be distinguished using different view directories, for example:

view common view layer directory
Mobile Access the view layer directory

In this way, different templates can use different page styles, and can also default to the view directory, as follows:
Copy code The code is as follows: 'DEFAULT_V_LAYER' => 'Mobile', // The default view layer name is changed to Mobile

3. Controller layerController

ThinkPHP controllers have two categories, one is the core controller and the other is the business controller. The core controller is in the ThinkPHP directory, such as thinkphpThinkPHPLibraryThinkControllerHproseController.class.php, which is responsible for the scheduling control of the application, including HTTP requests. interception, forwarding, loading configuration, etc. What we are going to discuss here is the business controller, which is completed by the user-defined controller class. The implementation principle of multi-layer business controller is similar to the layering of the model, such as business controller and event controller,

Controller/UserController //用于用户的业务逻辑控制和调度
Event/UserEvent //用于用户的事件响应操作

This event has not been used yet. It looks very high-end. There are very few user events in web development, and most of them are completed 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 response and responds through URL request, such as http://serverName/User/index. UserEvent is responsible for internal event response and can only call A('User','Event') internally; similarly we can Set default controller layer:
Copy code The code is as follows: 'DEFAULT_C_LAYER' => 'Event', // The default controller layer name is changed to Event
The inside and outside are isolated, and multi-layer controllers are not mandatory. They can be layered freely according to the needs of the application. Different layered models can be called in the controller as needed, and different layered views can also be displayed to achieve different implementations. theme.

In the three layers of MVC, ThinkPHP does not rely on M and V. It can have only C or only V. The user only needs to define the view, and it can automatically recognize it without C. However, this weird writing method will make Many programmers who have just started are very confused.

Multi-layer design has not been used in the current project, but I have seen it a lot in .net projects. I will add more next time it is used.

I hope this article will be helpful to everyone’s PHP programming based on the thinkPHP framework.

Articles you may be interested in:

  • Example analysis of ThinkPHP’s MVC development mechanism
  • ThinkPHP3.1 new features multi-layer MVC support
  • Share Ideas for solving related queries in ThinkPHP3.2
  • ThinkPHP3.2.2 plug-in controller function
  • ThinkPHP3.2.3 new database setting features
  • thinkphp3.2.2 realizes the generation of multiple thumbnails Method
  • Detailed explanation of the D method example of ThinkPHP3.1

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1086637.htmlTechArticleanalysis of thinkphp multi-layer MVC usage, thinkphp multi-layer mvc This article describes the usage of thinkphp multi-layer MVC. Share it with everyone for your reference, as follows: ThinkPHP supports multi-layer design. 1. Model...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn