You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 11无法自动注册事件与监听器问题求助

Laravel 11 事件监听器无法自动触发问题

我基于Laravel 11.x创建了新项目,通过以下CLI命令成功生成事件与监听器:

sail artisan make:event UserResetedPassword
sail artisan make:listener SendEmailUserResetPassword --event=UserResetedPassword

在User模型中配置了事件关联,希望用户创建时触发事件:

protected $dispatchesEvents = [
    'created' => UserResetedPassword::class
];

但监听器从未被调用。app.php文件未做修改,手动在AppServiceProvider.php中注册事件后,应用可正常识别并关联事件与监听器。

事件代码(UserResetedPassword.php)

<?php

namespace App\Events;

use App\Models\User;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class UserResetedPassword {
    use Dispatchable, InteractsWithSockets, SerializesModels;

    /**
     * Create a new event instance.
     */
    public function __construct(public readonly User $user) {
        //
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return array<int, Channel>
     */
    public function broadcastOn(): array {
        return [
            new PrivateChannel('channel-name'),
        ];
    }
}

监听器代码(SendEmailUserResetPassword.php)

<?php

namespace App\Listeners;

use App\Events\UserResetedPassword;
use App\Services\Mail\SendEmailUserCreatedPasswordService;

class SendEmailUserResetPassword {
    /**
     * Create the event listener.
     */
    public function __construct(private SendEmailUserCreatedPasswordService $service) {
        //
    }

    /**
     * Handle the event.
     */
    public function handle(UserResetedPassword $event): void {
        $this->service->sendEmailUserCreatedPassword($event->user);
    }
}

sail artisan event:list 输出结果

Filament\Events\ServingFilament ..................................................................................................................  
⇂ Closure at: /vendor/filament/filament/src/FilamentServiceProvider.php:89  
⇂ Closure at: /vendor/filament/filament/src/FilamentServiceProvider.php:89  
Illuminate\Auth\Events\Registered ................................................................................................................  
⇂ Illuminate\Auth\Listeners\SendEmailVerificationNotification  
Illuminate\Database\Events\QueryExecuted .........................................................................................................  
⇂ Spatie\LaravelIgnition\Recorders\QueryRecorder\QueryRecorder@record  
Illuminate\Foundation\Events\LocaleUpdated .......................................................................................................  
⇂ Closure at: /vendor/nesbot/carbon/src/Carbon/Laravel/ServiceProvider.php:57  
Illuminate\Foundation\Http\Events\RequestHandled .................................................................................................  
⇂ Closure at: /vendor/livewire/livewire/src/Features/SupportAutoInjectedAssets/SupportAutoInjectedAssets.php:24  
Illuminate\Log\Events\MessageLogged ..............................................................................................................  
⇂ Spatie\LaravelIgnition\Recorders\LogRecorder\LogRecorder@record  
Illuminate\Queue\Events\JobExceptionOccurred .....................................................................................................  
⇂ Spatie\LaravelIgnition\Recorders\JobRecorder\JobRecorder@record  
Illuminate\Queue\Events\JobProcessed .............................................................................................................  
⇂ Closure at: /vendor/spatie/laravel-ignition/src/IgnitionServiceProvider.php:262  
Illuminate\Queue\Events\JobProcessing ............................................................................................................  
⇂ Closure at: /vendor/laravel/framework/src/Illuminate/Log/Context/ContextServiceProvider.php:38  
⇂ Closure at: /vendor/spatie/laravel-ignition/src/IgnitionServiceProvider.php:256  
OwenIt\Auditing\Events\AuditCustom ...............................................................................................................  
⇂ OwenIt\Auditing\Listeners\RecordCustomAudit  
OwenIt\Auditing\Events\DispatchAudit .............................................................................................................  
⇂ OwenIt\Auditing\Listeners\ProcessDispatchAudit (ShouldQueue)  

我已尝试手动注册事件并生效,现在希望Laravel能自动识别并完成事件与监听器的注册。


内容的提问来源于stack exchange,提问作者j_gomes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 22:25:59