Automatic Event / Listener Discovery

Apr, 3 2019

Yesterday we shipped Laravel 5.8.9. Included in this release is the ability to have the framework automatically register your events and listeners. However, since this is a patch release, you must opt-in to this behavior. To do so, define the shouldDiscoverEvents method on your application's EventServiceProvider. To enable event discovery, this method should return true:

 * Determine if events and listeners should be automatically discovered.
 * @return bool
public function shouldDiscoverEvents()
  return true;

By default, all listeners within your application's Listeners directory will be scanned. If you would like to define additional directories to scan, you may override the discoverEventsWithin method in your EventServiceProvider.

In production, you likely do not want the framework to scan all of your listeners on every request. Therefore, during your deployment process, you should run the event:cache Artisan command to cache a manifest of all of your application's events and listeners. This manifest will be used by the framework to speed up the event registration process.

We hope you enjoy this new feature!

By Taylor Otwell

Creator of Laravel.

Follow the RSS Feed.