This article brings you relevant knowledge about thinkphp, which mainly introduces issues related to event binding, monitoring, and subscription. The advantage of events compared to middleware is that events are better than middleware For more precise positioning, let’s take a look at it below. I hope it will be helpful to everyone.
## Recommended study: "PHP Video Tutorial"
event.php) defines the monitoring of the corresponding event.
return [ 'bind' => [ 'UserLogin' => 'app\event\UserLogin', // 更多事件绑定 ], 'listen' => [ 'UserLogin' => ['app\listener\UserLogin'], // 更多事件监听 ], ];
will generate app\subscribe\ by default User class, or you can specify the full class name to generate. <?php namespace app\subscribe; class User { public function onUserLogin($user) { // UserLogin事件响应处理 } public function onUserLogout($user) { // UserLogout事件响应处理 } }
<?php namespace app\subscribe; use think\Event; class User { public function onUserLogin($user) { // UserLogin事件响应处理 } public function onUserLogout($user) { // UserLogout事件响应处理 } public function subscribe(Event $event) { $event->listen('UserLogin', [$this,'onUserLogin']); $event->listen('UserLogout',[$this,'onUserLogout']); } }
return [ 'bind' => [ 'UserLogin' => 'app\event\UserLogin', // 更多事件绑定 ], 'listen' => [ 'UserLogin' => ['app\listener\UserLogin'], // 更多事件监听 ], 'subscribe' => [ 'app\subscribe\User', // 更多事件订阅 ], ];
// 触发UserLogin事件 用于执行用户登录后的一系列操作 Event::trigger('UserLogin'); 或者使用助手函数 event('UserLogin');
PHP Video Tutorial》
The above is the detailed content of Completely master thinkphp's event binding, monitoring and subscription. For more information, please follow other related articles on the PHP Chinese website!