将$listen
变量更新为
protected $listen = [
'App\Events\UserActivationCode' => [
'App\Listeners\UserActivationCode\SendMailNotification',
'App\Listeners\UserActivationCode\SendSMSNotification',
]
];
在EventServiceProvider
中并正在运行
php artisan event:generate
命令,事件和侦听器已创建,并且我过去将它们部署到现在,当我将其更新为:
protected $listen = [
'App\Events\UserActivationCode' => [
'App\Listeners\UserActivationCode\SendMailNotification',
'App\Listeners\UserActivationCode\SendSMSNotification',
],
[
'App\Events\UserTracker' => [
'App\Listeners\UserTracker\StoreUserTracker',
]
]
];
再次运行命令,尽管我必须在Events
和Listeners
文件夹中手动创建该类,但不会创建该事件和侦听器,现在该事件对我不起作用>
路线测试:
Route::get('/page/mahdi', function () {
event(new \App\Events\UserTracker(\App\User::find(2),'hi',request()->ip()));
}
事件:
class UserTracker
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $user;
public $action_name;
public $user_ip;
public function __construct(User $user, $action_name, $user_ip)
{
$this->user = $user;
$this->action_name = $action_name;
$this->user_ip = $user_ip;
}
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}
监听器:
use App\Events\UserTracker;
class StoreUserTracker
{
public function __construct()
{
//
}
public function handle(UserTracker $event)
{
dd($event);
}
}
答案 0 :(得分:0)
那里的方括号太多。删除您的UserTracker
事件周围的事件:
protected $listen = [
'App\Events\UserActivationCode' => [
'App\Listeners\UserActivationCode\SendMailNotification',
'App\Listeners\UserActivationCode\SendSMSNotification',
],
'App\Events\UserTracker' => [
'App\Listeners\UserTracker\StoreUserTracker',
],
];