laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.27k stars 10.94k forks source link

Laravel 11 events are not auto discovered outside app folder #52975

Open xizprodev opened 1 day ago

xizprodev commented 1 day ago

Laravel Version

^11.9

PHP Version

8.3.9

Database Driver & Version

-

Description

Reproduction repo url: https://github.com/xizprodev/laravel-with-events.

Description: Laravel does not allow "Listeners" to be discovered automatically if they are not in the app folder.

https://github.com/xizprodev/laravel-with-events/blob/master/app/Listeners/SendPodcastNotification.php - auto discovered (in the app folder)

https://github.com/xizprodev/laravel-with-events/tree/master/custom-namespace/FolderName/Listeners - is not auto discovered

Following documentation: https://laravel.com/docs/11.x/events#dispatching-events.

https://github.com/xizprodev/laravel-with-events/blob/master/bootstrap/app.php - has the ->withEvents with required folder path.

I think this is a bug because it is not specified in the docs that only the app folder will be scanned.

"If you plan to store your listeners in a different directory or within multiple directories, you may instruct Laravel to scan those directories using the withEvents method in your application's bootstrap/app.php file:"

Steps To Reproduce

git clone git@github.com:xizprodev/laravel-with-events.git php artisan event:list

only App\Events\PodcastProcessed appear.

xizprodev commented 1 day ago

Found a fix: https://github.com/laravel/framework/pull/52976

Ok, so I found the problem. The method DiscoverEvents::classFromFile return Custom-namespace\FolderName\Listeners\CustomListenerName instead of CustomNamespace\FolderName\Listeners\CustomListenerName

So my folder structure is like:

app bootstrap ... modules modules-core (like custom-namespace in repo example)

And the issue is with naming. But I think these folders should work. It's more of a preference.