Home >PHP Framework >ThinkPHP >ThinkPHP6 events and multiple applications
Event
1. Events are somewhat similar to middleware, except that events can more accurately locate more detailed business scenarios;
2. Events can be defined : event class, event listening class, event subscription class;
3. We first create a test event class: TestEvent.php, and manually create a test class;
public function __construct() { //注册监听器 Event::listen('TestListen', function ($param) { echo '我是监听器,我被触发了!'.$param; }); } public function info() { echo '登录前准备!'; Event::trigger('TestListen', 'ok'); //触发监听器 event('TestListen'); //助手函数触发 }
4. We also You can use the listening class to design the listener and create it using the command line;
php think make:listener TestListen
public function info() { echo '登录前准备!'; Event::listen('TestListen', TestListen::class); //这句可以定义到配置文件 Event::trigger('TestListen'); }
5. In app/event.php, listen is configured to listen to 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 '我是监听类!'.$event; }
7. The system also has built-in system-triggered events, which will automatically trigger as long as the conditions are met;
Event description parameters AppInit application initialization tag bit None HttpRun application start tag bit None HttpEnd application end tag bit Current response object instance LogWrite log write method tag bit Currently written log information RouteLoaded Route loading completed None
8. The event listening class can monitor multiple listening classes at the same time, as long as it is bound to an identifier;
'TestListen' => [ \app\listener\TestListen::class, \app\listener\TestOne::class, \app\listener\TestTwo::class ]
9. For those who need multiple monitoring, the listening class is not flexible enough. And there will be a lot of classes created, you can use the subscription class;
10. The subscription class is to use the on method name to monitor events as an internal method;
php think make:subscribe UserSub class UserSub { public function onUserLogin(){ echo '处理登录后的监听!'; } public function onUserLogout(){ echo '处理退出后的监听!'; } }
11. Then, we go directly Register in app/event.php;
'subscribe' => [ 'UserSub' => \app\subscribe\UserSub::class, ],
12. Then, the two methods listen to the two event methods respectively, and just call the method name directly;
public function login(){ echo '登录成功!'; Event::trigger('UserLogin'); } public function logout(){ echo '退出成功!'; Event::trigger('UserLogout'); }
13. For event classes, it is very There are few scenarios where it is necessary to use it. After all, the system provides many precise solutions;
php think make:event UserEvent
class UserEvent { public function __construct() { echo '我是事件类!'; } } Event::trigger(new UserEvent());
Multi-application mode
1. Since the multi-application mode is an extension, we Additional installation is required;
composer require topthink/think-multi-app
2. After installation, create two application directory folders, index and admin;
3. Just move the controller and model in and modify the corresponding namespace;
4. Add two application directory folders, index and admin, to the view and move them into the corresponding folders;
5. The default application is index, which can be modified in app.php;
// 默认应用 'default_app' => 'index',
6. We can do application mapping, such as mapping the admin directory to think, and admin is abandoned;
// 应用映射(自动多应用模式有效) 'app_map' => [ 'think' => 'admin' ],
7. We can also do domain name binding, for example, using domain name binding in the background, Direct access;
// 域名绑定(自动多应用模式有效) 'domain_bind' => [ 'news.abc.com' => 'admin', '*' => 'index' ],
8. Route modification: The route needs to be established separately in the application directory, and the internal coding does not need to be changed;
Recommended tutorial: "ThinkPHP Tutorial"
The above is the detailed content of ThinkPHP6 events and multiple applications. For more information, please follow other related articles on the PHP Chinese website!