Laravel 8 中未收听内置事件

Built-in events are not being listened in Laravel 8

我正在从我的控制器触发一些内置事件。但是没有事件正在被(收听和)回应。我已按照 this doc 设置密码重置功能,但 \Illuminate\Auth\Events\PasswordReset 事件不起作用。我的期望是它应该发送确认电子邮件。以下是我的代码:

routes/web.php:

//...
Route::get('/reset-password/{token}', [AuthController::class, 'reset'])->middleware('guest')->name('password.reset');
Route::post('/reset-password', [AuthController::class, 'shiny'])->middleware('guest')->name('password.update');

app/Http/Controllers/AuthController.php:

//...
public function reset(string $token): View
{
    return view('auth/password/reset', ['token' => $token]);
}

public function shiny(ResetUserRequest $request): RedirectResponse
{
    $status = Password::reset(
        $request->only('email', 'password', 'password_confirmation', 'token'),
        function ($user, $password) use ($request) {
            $user->forceFill(['password' => $password])->save();
            if (!empty($user->getRememberToken())) {
                $user->setRememberToken(Str::random(60));
            }
            event(new PasswordReset($user)); // not working
        }
    );
    if ($status !== Password::PASSWORD_RESET) {
        return back()->withErrors(['email' => __($status)]);
    }
    return redirect()->route('auth.signin')->with('status', __($status));
}

但只有一个事件得到响应:\Illuminate\Auth\Events\Registered。没有其他事件得到响应。我怀疑发生这种情况是因为 \App\Providers\EventServiceProvider 提供程序中没有为其他事件(除了那个事件)提供侦听器。这是我搭建项目时得到的:

namespace App\Providers;

use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],
        // no more listeners for other types of events
        // could this be the reason?
    ];

    /**
     * Register any events for your application.
     *
     * @return void
     */
    public function boot()
    {
        //
    }
}

我基本上是一名 JS 开发人员,所以这一切对我来说似乎很复杂。客户特别要求我使用Laravel来做,所以这就是问题所在。有人可以帮我吗?

  1. 其他类型的事件不再有侦听器。您应该添加所需的听众。
  2. 会不会是这个原因?是的。

它会是这样的:

namespace App\Providers;
use Illuminate\Auth\Events\PasswordReset; // Pay attention to the namespace 
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],
        PasswordReset::class => [
            //Listiners that you need to be called after triggering the PasswordReset event.
        ]

    ];