Home >Backend Development >PHP Tutorial >About the use of multi-layer controllers and multi-level controllers in the thinkPHP framework

About the use of multi-layer controllers and multi-level controllers in the thinkPHP framework

不言
不言Original
2018-06-14 15:29:572679browse

This article mainly introduces the use of TP (thinkPHP) framework multi-layer controllers and multi-level controllers, and analyzes the structure, principles and usage of thinkPHP framework multi-layer controllers and multi-level controllers in the form of examples. Friends in need can refer to

This article describes the use of multi-layer controllers and multi-level controllers in the TP (thinkPHP) framework. Share it with everyone for your reference, the details are as follows:

The directory structure of the multi-layer controller is as follows:

├─ Controller Access Controller
│ ├─UserController.class.php
│ ├─BlogController.class.php
│ ...
├─Event Event Controller
│ ├─UserEvent. class.php
│ ├─BlogEvent.class.php
│ ...

Add multi-layer controllers to the App application. No need to add any parameters, just follow the above Just create the file directly.

Multi-level controller instantiation:

/*
 * 多层控制器实例化 即和访问控制器Controller同级目录的Service时 可用D方法或A方法
 * 注:访问控制器的名称是通过DEFAULT_C_LAYER设置的,默认是Controller,访问控制器负责外部交互响应,
    其它的控制器层和外部隔离,只能内部调用。定义其他的控制器层,则不一定必须要继承系统的Controller类或其子类,通常需要输出模版的时候才需要继承Controller类。
 */
D('Admin', 'Service');
//或者
A('User','Event');
A('Admin/Blog','Event');  //假设当前模块是Home模块
// 实例化Home模块的User事件控制器
$User = new \Home\Event\UserEvent();
// 实例化Admin模块的Blog事件控制器
$Blog = new \Admin\Event\BlogEvent();

And the multi-level controller The directory structure is like this:

├─Controller access controller
│ ├─User User classification (group)
│ │ ├─UserTypeController.class. php
│ │ ├─UserAuthController.class.php
│ ...
│ ├─Admin Admin rating (group)
│ │ ├─UserController.class.php
│ │ ├ ─ConfigController.class.php
│ ...

Its access format:

http://serverName/Home/User/UserType
http://serverName/Home/Admin/User

Setting up a multi-level controller requires setting configuration parameters, that is, setting the hierarchical level of the controller, for example, setting level 2 The controller layer of the directory is as follows:

'CONTROLLER_LEVEL'   => 2,

The command space is like this;

<?php
namespace Home\Controller\Admin;
use Think\Controller;
class IndexController extends Controller {
  public function hello(){
    echo &#39;hello&#39;;
  }
  public function test(){
    echo &#39;test&#39;;
  }
}

Instancing of multi-level controllers:

can be instantiated directly

// 实例化Home模块的User控制器
$User = new \Home\Controller\User\UserTypeController();
// 实例化Admin模块的Blog控制器
$Blog = new \Admin\Controller\Admin\UserController();

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

About the usage of custom configuration and loading of ThinkPHP3.2 framework

##How does thinkPHP framework implement multiple Table query

The above is the detailed content of About the use of multi-layer controllers and multi-level controllers in the thinkPHP framework. For more information, please follow other related articles on the PHP Chinese website!

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