-
//Defining a custom Event class is not necessarily an event class, it is OK for ordinary classes, but using event class architecture is clearer
-
//The following will generate a UserLogin event class in the directory app\event
-
php think make:event UserLogin
Write the code as follows:
-
namespace app\event;
-
class UserLogin
-
{
-
//Hand method name and parameters are customized
-
public function sendMessage($param)
-
{
-
//Processing code
-
echo('sendmessage');
-
}
-
}
-
class UserLoginSubscribe
-
{
-
// Manual event subscription, the method name must be subscribe, and add events to the event subscriber in the function
-
public function subscribe(Event $event)
-
{
-
//Add events to subscriber The first parameter is the event identifier, write casually, the second parameter is the event class and the executed function
-
$event->listen('UserLogin',[app('app\event\UserLogin'),'sendMessage']);
-
-
//You can also dynamically modify the event logo for the automatic identification function onLogout. The original logo is Logout, but now it is changed to Out
-
//$event->listen('Out', [$this,'onUserLogout']);
-
}
-
-
}
-
return [
-
'subscribe' => [
-
'app\subscribe\UserLoginSubscribe',
-
// More event subscriptions
-
],
-
];
-
//Call, the event identifier is the first parameter of $event->listen('UserLogin',[app('app\event\UserLogin'),'sendMessage'])
-
event('UserLogin');