Use events and listeners in a module
To run a listener when the Blog module publishes a post, generate the event and listener and map them in the module’s EventServiceProvider. You need an enabled Blog module; module:make creates Modules/Blog/app/Providers/EventServiceProvider.php and registers it in the main provider.
-
Generate the event.
Terminal window php artisan module:make-event PostPublished BlogThis creates
Modules/Blog/app/Events/PostPublished.php(Modules\Blog\Events\PostPublished). -
Generate the listener.
--eventtypes thehandle()parameter, and--queuedmakes it implementShouldQueue.Terminal window php artisan module:make-listener NotifySubscribers Blog --event=PostPublished --queuedThis creates
Modules/Blog/app/Listeners/NotifySubscribers.phpwithhandle(PostPublished $event).A short name like
PostPublishedresolves in the module’sEventsnamespace. Names starting with\, the application namespace (App\),Illuminate\, or the configuredmodules.namespace(Modules\) are used as fully qualified class names without needing a leading\. -
Map the event to the listener in
$listen.Modules/Blog/app/Providers/EventServiceProvider.php protected $listen = [\Modules\Blog\Events\PostPublished::class => [\Modules\Blog\Listeners\NotifySubscribers::class,],]; -
Dispatch the event when a post is published.
use Modules\Blog\Events\PostPublished;PostPublished::dispatch();Queued listeners require a running worker (
php artisan queue:work) unlessQUEUE_CONNECTION=sync. -
Check the result.
Terminal window php artisan event:list --event=PostPublishedModules\Blog\Events\PostPublished ..........................................⇂ Modules\Blog\Listeners\NotifySubscribers (ShouldQueue)
Event discovery and modules
Section titled “Event discovery and modules”The generated EventServiceProvider registers only its $listen array and does not scan for listeners. By default, Laravel scans only app/Listeners. Remove any legacy $shouldDiscoverEvents property from older module providers, as it has no effect.
To discover module listeners automatically, add module listener folders to withEvents() in bootstrap/app.php:
->withEvents(discover: [ __DIR__.'/../app/Listeners', __DIR__.'/../Modules/*/app/Listeners',])Keep these rules in mind when using event discovery:
withEvents(discover: [...])replaces Laravel’s default path, so includeapp/Listenersalongside your module glob.- The package resolves module listener file paths to their module class names automatically.
- Globs match disabled modules as well (see Module status and activators).
- Do not list a discovered listener in
$listen, or it runs twice. - Re-run
php artisan event:cacheafter changing mappings. See Changes in 13.x.
Register a listener without EventServiceProvider
Section titled “Register a listener without EventServiceProvider”You can also register a listener in the module service provider’s boot() method. Always call parent::boot():
use Illuminate\Support\Facades\Event;use Modules\Blog\Events\PostPublished;use Modules\Blog\Listeners\NotifySubscribers;
public function boot(): void{ parent::boot();
Event::listen(PostPublished::class, NotifySubscribers::class);}