Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use event binding, listening, and subscribing for thinkphp

Shulou Source: shulou.com Published: 2022-05-31 23:06:05 09月21日 Update

This article mainly introduces thinkphp event binding, monitoring and subscription related knowledge, the content is detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that after reading this thinkphp event binding, monitoring and subscription how to use the article will have a harvest, let's take a look.

What is the event?

The advantage of event over middleware is that events are more accurately located (or finer-grained) than middleware, and are more suitable for the expansion of some business scenarios. For example, we usually encounter that users need to do a series of operations after registering or logging in. Through the event system, we can not invade the original code to complete the login operation expansion, reduce the coupling of the system, but also complete the business requirements.

The use of events in TP6

According to the explanation on the official website, there are no examples. Here is a simple way to tell you how to use it. You will find more operations after using it.

1. Event monitoring

Command line generation monitor php think make:listener UserLogin

It is generally recommended to define snooping for corresponding events directly in the event definition file (event.php).

Return ['bind' = > [' UserLogin' = > 'app\ event\ UserLogin', / / more event binding],' listen' = > ['UserLogin' = > [' app\ listener\ UserLogin'], / / more event listeners],]; 2. Event subscription

Command line php think make:subscribe User

The app\ subscribe\ User class is generated by default, or you can specify the full class name to generate.

Tags: Events monitoring subscriptions more knowledge generation login business middleware content commands files methods users articles systems processing precision code value Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info NVidia MySQL Xiaomi vpn