ThinkPHP6 events and multiple applications

Keywords: PHP



1. Events are similar to middleware, but events are more accurately positioned and more detailed business scenarios;

2. Event definable: event class, event listening class and event subscription class;

3. We first create a test event class: TestEvent.php, and manually create a test class;

public function __construct()


//Register listener

Event::listen('TestListen', function ($param) {

echo 'I'm a monitor, I'm triggered!'.$param;



public function info()


echo 'Prepare before login!';

Event::trigger('TestListen', 'ok'); //Trigger monitor

event('TestListen'); //Helper function trigger




4. We can also use listener classes to design listeners and create them on the command line;

php think make:listener TestListen
public function info()
echo 'Prepare before login!';
Event::listen('TestListen', TestListen::class); //This sentence can be defined in the configuration file


5. In app/event.php, listen is used to configure the listening class. The configuration method is as follows:

'listen' => [
'TestListen' => [\app\listener\TestListen::class]


6. When the listening class is triggered, it will automatically execute the handle() method to implement the listening function;

public function handle($event)
echo 'I'm a monitor!'.$event;


7. The system also has built-in events triggered by the system, which will be triggered automatically as long as the conditions are met;

Event description parameter AppInit application initialization tag bit no HttpRun application start tag bit no HttpEnd application end tag bit current response object instance LogWrite log write method tag bit current write log information RouteLoaded route loading complete no

8. event listening class. You can listen to multiple listening classes at the same time as long as they are bound to an identifier;

'TestListen' => [


9. For multiple listening, the listening class is not flexible enough, and many classes will be created. You can use subscription classes;

10. The subscription class is to implement the listening event as an internal method with the on + method name;

php think make:subscribe UserSub
class UserSub
public function onUserLogin(){
echo 'Handle listening after login!';
public function onUserLogout(){
echo 'Process the monitoring after exiting!';


11. Then, we go directly to app/event.php to register;

'subscribe' => [
'UserSub' => \app\subscribe\UserSub::class,


12. Then, the two methods listen to two event methods respectively, and directly call the method name;

public function login(){
echo 'Login succeeded!';
public function logout(){
echo 'Exit succeeded!';


13. For event class, few scenarios need to use it, after all, the system provides many precise schemes;

php think make:event UserEvent
class UserEvent
public function __construct()
echo 'I'm event class!';
Event::trigger(new UserEvent());


Multi application mode

1. As the multi application mode is an extension, we need to install it additionally;

composer require topthink/think-multi-app


2. After installation, create two application directory folders, index and admin;

3. As long as the controller and model are moved in, the corresponding namespace can be modified;

4. Add two application directory folders index and admin to view and move them into corresponding folders;

5. The default application is index, which can be modified in app.php;

// Default application
'default_app' => 'index',


6. We can do application mapping, such as mapping the admin directory to think, and discarding admin;

// Application mapping (automatic multi application mode is effective)
'app_map' => [
'think' => 'admin'


7. We can also do domain name binding. For example, the background uses domain name binding to access directly;

// Domain name binding (automatic multi application mode is effective)
'domain_bind' => [
'' => 'admin',
'*' => 'index'


8. Route modification: the route needs to be established separately in the application directory, and the internal code does not need to be changed;

For more information, please visit:

BA CHONGYING: the complete tutorial catalog of Tencent T3-T4 standard boutique PHP architect, as long as you read it, you can guarantee a higher salary (continuous update)

Posted by why2k on Fri, 08 May 2020 03:58:55 -0700