laravel - 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 来做这件事,所以这就是问题所在。有人可以帮我吗?
解决方案
- 不再监听其他类型的事件。您应该添加所需的侦听器。
- 这可能是原因吗?是的。
它会是这样的:
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.
]
];
推荐阅读
- javascript - meteor facts package shows nothing, if I include facts-base I get "Ignoring duplicate publish"
- python - Unable to add comments to a post using a ModelForm in Django
- javascript - JQUERY Show overlay when button click work only first time. What's wrong?
- javascript - Java put/putShort 与 JS
- python - 无效参数:无法终止已退出进程,使用 geckodriver 在 Python 中运行 Selenium
- android - 如何在用于开发的安卓手机上退出恢复模式循环?
- vue.js - 如何从“props”数组中拼接一个对象并将其分配给预定义的“data”属性,使其可访问和反应?
- android - 将 LocalDao getData() 与 ViewModel 一起使用时接收 null
- html - 将一个组件垂直居中,并将 appbar 置于屏幕材质 UI 的顶部
- r - 分数向量(来自 MASS)如何进行错误检查?