Laravel添加了新事件,并且听众无法正常工作

时间:2018-07-26 20:43:05

标签: php laravel-5.6

$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',
        ]
    ]
];

再次运行命令,尽管我必须在EventsListeners文件夹中手动创建该类,但不会创建该事件和侦听器,现在该事件对我不起作用

路线测试:

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);
    }
}

1 个答案:

答案 0 :(得分:0)

那里的方括号太多。删除您的UserTracker事件周围的事件:

protected $listen = [
    'App\Events\UserActivationCode' => [
        'App\Listeners\UserActivationCode\SendMailNotification',
        'App\Listeners\UserActivationCode\SendSMSNotification',
    ],
    'App\Events\UserTracker' => [
        'App\Listeners\UserTracker\StoreUserTracker',
    ],
];