duanmeng3126 2019-07-31 08:20
浏览 606

如何在boot()方法内手动注册Laravel 5.8中的内置身份验证事件

I'm working with Laravel authentication. By default several events are fired during the authentication process. Listeners for these events are configured in EventServiceProvider.php:

/** 
 * The event listener mappings for the application. 
 * 
 * @var array 
 */
protected $listen = [
    'Illuminate\Auth\Events\Registered' => [
        'App\Listeners\LogRegisteredUser',
    ],
    'Illuminate\Auth\Events\Attempting' => [
        'App\Listeners\LogAuthenticationAttempt',
    ],
    'Illuminate\Auth\Events\Authenticated' => [
        'App\Listeners\LogAuthenticated',
    ],
    'Illuminate\Auth\Events\Login' => [
        'App\Listeners\LogSuccessfulLogin',
    ],
];

To manually configure listeners for events, I have to use the listen static method still inside EventServiceProvider.php:

/**
 *Register any other events for your application.
 *
 * @return void
 */
public function boot() {
    parent::boot();
    Event::listen('event.name', function($foo, $bar) {
        //
    });
}

Problem: I want to configure a manual listener for login events instead of using the $listen array and create a session variable in response to successful logins like this:

public function boot() {
    parent::boot();

    Event::listen('event.login', function($guard, $user, $remember) {
        session(['notification' => 'login successful']);
    });
}

When I do this, the login event is never caught after a successful login.

But if I use a wildcard to set the name of the event like this:

public function boot() {
    parent::boot();

    Event::listen('*', function($eventName, array $data) {
        session(['notification' => 'login successful']);
    });
}

the listener successfully fires, however for every possible event.

Question: How do i get the names of inbuilt laravel events and how do I create a listener manually for inbuilt events (E.g. authentication events like login) inside the boot method of EventServiceProvider.php, without including them in the $listen array? The reason I want to do this is so I do not generate any extra files using php artisan event:generate command

  • 写回答

1条回答 默认 最新

  • dongshang1979 2019-07-31 10:48
    关注

    Cartalyst Sentinel package may help a lot in customized and easy authentication.

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘