首页 > 解决方案 > Laravel 8 中没有监听内置事件

问题描述

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

路线/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');

应用程序/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 来做这件事,所以这就是问题所在。有人可以帮我吗?

标签: laravellaravel-8

解决方案


  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.
        ]

    ];

推荐阅读