Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Gonzalo2683's avatar

Issues Registering Event Listeners in Laravel 11 Using withEvents()

Hello community,

I'm working on an application with Laravel 11 and have encountered an issue while trying to register event listeners following the documentation's recommendations. Specifically, I'm attempting to use withEvents() in the bootstrap/app.php file with a namespace similar to the following:

->withEvents(discover: [
  __DIR__.'/../src/app/Notification/Listeners',
])

My goal is to automate the discovery of listeners without having to manually register each one. However, after configuring withEvents() as shown above, I run the php artisan event:list command to check if the listeners are recognized, and I realize that they are not listed.

Interestingly, I manage to get the listeners recognized only when I manually register them in the boot() method of a specific provider class.

Here are some points I've checked:

  1. Path and Namespace: I've made sure the path and namespace are correct.
  2. Documentation: I've followed the official Laravel 11 documentation recommendations for implementation.
  3. Configuration Cache: I've cleared the configuration cache after making changes using php artisan config:clear.

Has anyone faced a similar issue or has any idea why the listeners are not being automatically registered through withEvents()? Any suggestion or help would be greatly appreciated.

Thanks.

0 likes
6 replies
LaryAI's avatar
Level 58

It seems like you're on the right track with using withEvents() to automatically discover event listeners in Laravel 11. However, there might be a slight issue with the syntax or the way the discovery is being set up. Here's a step-by-step solution to ensure that your event listeners are discovered and registered correctly:

  1. Check the Directory Structure: Ensure that your listeners are in the correct directory and that they are namespaced properly. The directory should match the namespace you are using.

  2. Listener Class Structure: Make sure that each listener class within the directory is properly structured with the correct namespace and extends the base listener class if necessary.

  3. Use the Correct Method: The withEvents() method should be called on the application instance in bootstrap/app.php. Make sure you are using the correct method and that it's being called in the right place.

  4. Correct Syntax: The withEvents() method expects an array of directories, not a discover: key. Here's how you should structure the call:

$app->withEvents([
    __DIR__.'/../src/app/Notification/Listeners',
]);
  1. Clear Caches: After making changes, clear all relevant caches. You've mentioned clearing the configuration cache, but it's also a good idea to clear the event cache:
php artisan config:clear
php artisan event:clear
  1. Check Autoloading: Ensure that Composer's autoloader is aware of your listeners. You might need to dump the autoloader:
composer dump-autoload
  1. Debugging: If the listeners are still not being registered, you may need to debug further. Check if the directory is being read by adding some logging to the bootstrap/app.php file temporarily to ensure the path is correct and accessible.

  2. Documentation: Double-check the Laravel 11 documentation to ensure that there haven't been any changes to the way event listeners are registered since your last review.

If you've gone through all these steps and the listeners are still not being registered, consider reaching out to the Laravel community or checking the Laravel GitHub repository for any reported issues that might be similar to yours. There could be a bug or a change in the framework that hasn't been documented yet.

puklipo's avatar

Write your directory structure and composer.json.

Is this really the structure?

  • bootstrap/
  • src/
  • composer.json
Gonzalo2683's avatar

Yes, the structure is as you mentioned.

"autoload": {
        "psr-4": {
            "App\": "app/",
            "Database\Factories\": "database/factories/",
            "Database\Seeders\": "database/seeders/",
            "CustomApp\": "src/app/"
        }
    },

It appears that reorganization is only effective within the app folder(laravel app folder), and attempts to do so from a different namespace, even after registering it in composer, may not work as expected.

A key point about using withEvents() is that once a specific location for listeners is defined using this method, Laravel stops searching in the default locations (app/Listeners). This means that if you want Laravel to also consider listeners in the default locations, you would need to explicitly register those paths in addition to the custom one you've already defined with withEvents().

1 like
jekinney's avatar

100% and to add on: While I would argue Laravel is the least opinionated, it still is opinionated. Meaning some times configuring "stuff" to work outside the box means you need to be consistent for two reasons:

  1. Time and effort. Is it worth it? How many hours have you spent trying to make this work compared to default or a simpler approach
  2. Can slow down the application. Even a few milliseconds can add up as you add more searching and figuring out outside of defaults and not to mention overrides. If you have 40 "things" causing 5 ms delay MIGHT add 200 ms right there. I try to keep as many responses with out caching to under 300ms on load tests. Otherwise I look at adding caching and other performance helping methods.

This is just and FYI and obviously my opinion and work flow.

Gonzalo2683's avatar

I understand your point, yes, it took me some time. Although I have known about Laravel as a technology for several years now, I am quite new to its consistent use and I've been personally using it in my spare time to learn. However, this time invested has helped me to better understand how it works, and I believe that while it was challenging for me, it might also be saving some time for others, possibly including you @jekinney . Having a perspective like the one you've expressed also helps, it is appreciated.

musheabdulhakim's avatar

You can manually register events in laravel 11 in the boot method of your AppServiceProvider

Event::listen(
            EventClass::class,
            ListenerClass::class,
        );

with this, The EventClass has been mapped to ListenerClass

Please or to participate in this conversation.