-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Description
Versions:
- laravel-modules Version: 12.0.4
- Laravel Version: 12.34.0
- PHP Version: 8.4
Description:
Laravel 12 introduced a new withEvents() method in bootstrap/app.php to configure event discovery directories. According to the Laravel 12 documentation, you can
specify custom listener directories using wildcards:
->withEvents(discover: [
__DIR__.'/../app/Listeners',
__DIR__.'/../Modules/*/app/Listeners', // Should discover all module listeners
])However, this does not work for discovering listeners in module directories when using nwidart/laravel-modules. Listeners in module directories (e.g., Modules/Loyverse/app/Listeners/SyncLoyversePoints.php) are not auto-discovered for events in the main application (e.g., App\Events\LoyaltyPointsAwarded), even though:
- The listener has a proper handle(
EventClass $event) method signature - The module's EventServiceProvider has
$shouldDiscoverEvents = true - The path is correctly specified in withEvents()
The only workaround is to manually register the event-listener mapping in the module's EventServiceProvider:
protected $listen = [
\App\Events\LoyaltyPointsAwarded::class => [
\Modules\Loyverse\Listeners\SyncLoyversePoints::class,
],
];This defeats the purpose of Laravel 12's improved auto-discovery feature.
Steps To Reproduce:
- Install a fresh Laravel 12 application
- Install nwidart/laravel-modules:
composer require nwidart/laravel-modules - Create a module:
php artisan module:make Loyverse - Create an event in the main app:
php artisan make:event LoyaltyPointsAwarded - Create a listener in the module at
Modules/Loyverse/app/Listeners/SyncPoints.php
<?php
namespace Modules\Loyverse\Listeners;
use App\Events\LoyaltyPointsAwarded;
class SyncPoints
{
public function handle(LoyaltyPointsAwarded $event): void
{
\Log::info('Listener triggered');
}
}- Configure event discovery in bootstrap/app.php:
->withEvents(discover: [
__DIR__.'/../app/Listeners',
__DIR__.'/../Modules/*/app/Listeners',
])- Ensure the module's EventServiceProvider is registered and has
$shouldDiscoverEvents = true - Clear caches:
php artisan optimize:clear - Check registered listeners:
php artisan event:list
Expected Result: The SyncPoints listener should appear under LoyaltyPointsAwarded event.
Actual Result: The listener is not discovered.