TP of thinkPHP Framework Multilayer Controller and Multilevel Controller Use Example

  • 2021-10-16 01:14:43
  • OfStack

This paper illustrates the use of multi-layer controller and multi-level controller in TP (thinkPHP) framework. Share it for your reference, 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 controller to App application, without adding any parameters, just build files directly according to the above method.

Multilayer controller instantiation:


/*
 *  Instantiation of multilayer controller   Instant and access controller Controller Of the sibling directory Service Hour   Available D Method or A Method 
 *  Note: The name of the access controller is passed through DEFAULT_C_LAYER Set, the default is Controller The access controller is responsible for external interactive response, 
     Other controller layers are isolated from the outside and can only be called internally. Define other controller layers, do not 1 You must inherit the system Controller Class or its subclasses, you usually need to inherit when you need to output templates Controller Class. 
 */
D('Admin', 'Service');
// Or 
A('User','Event');
A('Admin/Blog','Event');  // Assume that the current module is Home Module 
//  Instantiation Home Modular User Event controller 
$User = new \Home\Event\UserEvent();
//  Instantiation Admin Modular Blog Event controller 
$Blog = new \Admin\Event\BlogEvent();

The directory structure of the multilevel controller is as follows:

Controller Access Controller
→ User User Rating (Group)
→ → UserTypeController. class. php
→ → UserAuthController. class. php
...
→ Admin Admin Classification (Group)
→ → UserController.class.php
→ → ConfigController. class. php
...

Its access format:

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

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


'CONTROLLER_LEVEL'   => 2,

Its command space is like this;


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

Instantiation of multilevel controllers:

You can instantiate it directly


//  Instantiation Home Modular User Controller 
$User = new \Home\Controller\User\UserTypeController();
//  Instantiation Admin Modular Blog Controller 
$Blog = new \Admin\Controller\Admin\UserController();

For more readers interested in thinkPHP related contents, please check the topics of this site: "ThinkPHP Introduction Tutorial", "thinkPHP Template Operation Skills Summary", "ThinkPHP Common Methods Summary", "codeigniter Introduction Tutorial", "CI (CodeIgniter) Framework Advanced Tutorial", "Zend FrameWork Framework Introduction Tutorial" and "PHP Template Technology Summary".

Hope that this article is based on the framework of ThinkPHP PHP programming help.


Related articles: