Skip to content

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.

  1. Generate the event.

    Terminal window
    php artisan module:make-event PostPublished Blog

    This creates Modules/Blog/app/Events/PostPublished.php (Modules\Blog\Events\PostPublished).

  2. Generate the listener. --event types the handle() parameter, and --queued makes it implement ShouldQueue.

    Terminal window
    php artisan module:make-listener NotifySubscribers Blog --event=PostPublished --queued

    This creates Modules/Blog/app/Listeners/NotifySubscribers.php with handle(PostPublished $event).

    A short name like PostPublished resolves in the module’s Events namespace. Names starting with \, the application namespace (App\), Illuminate\, or the configured modules.namespace (Modules\) are used as fully qualified class names without needing a leading \.

  3. 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,
    ],
    ];
  4. 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) unless QUEUE_CONNECTION=sync.

  5. Check the result.

    Terminal window
    php artisan event:list --event=PostPublished
    Modules\Blog\Events\PostPublished ..........................................
    ⇂ Modules\Blog\Listeners\NotifySubscribers (ShouldQueue)

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:

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 include app/Listeners alongside 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:cache after 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():

Modules/Blog/app/Providers/BlogServiceProvider.php
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);
}