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

getupkid203's avatar

Event Auto Discovery Laravel 11 Not Working

Hi all, As per the Laravel 11 docs: https://laravel.com/docs/master/events#event-discovery

By default, Laravel will automatically find and register your event listeners by scanning your application's Listeners directory. When Laravel finds any listener class method that begins with handle or __invoke, Laravel will register those methods as event listeners for the event that is type-hinted in the method's signature:

This does not seem to be working for me. I have my listener and event in the default namespaces. Even when I try ->withEvents(), it does not register correctly. They only register correctly when I manually register the events in AppServiceProvider but I don't really want to have to do this for every single event.

Any help appreciated.

<?php

namespace App\Listeners;

use App\Events\PostPublished;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class ProcessPostPublication
{
    /**
     * Handle the event.
     */
    public function handle(PostPublished $event): void
    {
        //
    }
}
<?php

namespace App\Events;

//...

class PostPublished
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    /**
     * Create a new event instance.
     */
    public function __construct()
    {
        //
    }
}

0 likes
1 reply
LaryAI's avatar
Level 58

To troubleshoot the issue with event auto-discovery in Laravel 11, you can follow these steps to ensure everything is set up correctly:

  1. Check Directory Structure: Ensure that your event and listener classes are located in the correct directories. By default, Laravel expects events to be in the App\Events namespace and listeners in the App\Listeners namespace.

  2. Event and Listener Naming: Make sure that your listener class has a method named handle or __invoke that type-hints the event class in its signature. You've done this correctly in your ProcessPostPublication listener.

  3. Composer Autoload: Run composer dump-autoload to ensure that all classes are properly autoloaded. This can sometimes resolve issues where Laravel is unable to discover classes.

  4. Configuration Cache: If you have configuration caching enabled, it might interfere with auto-discovery. Clear the configuration cache using:

    php artisan config:clear
    
  5. Event Service Provider: Ensure that your EventServiceProvider is set up correctly. It should look something like this:

    <?php
    
    namespace App\Providers;
    
    use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
    
    class EventServiceProvider extends ServiceProvider
    {
        /**
         * The event listener mappings for the application.
         *
         * @var array
         */
        protected $listen = [
            // Manually registered events can go here, but should be empty for auto-discovery
        ];
    
        /**
         * Register any events for your application.
         */
        public function boot()
        {
            parent::boot();
        }
    }
    
  6. Check for Errors: Look for any errors in your logs that might indicate why the auto-discovery is failing. You can check the storage/logs/laravel.log file for any relevant error messages.

  7. Laravel Version: Ensure that you are indeed using Laravel 11, as features can vary between versions. You can check your Laravel version with:

    php artisan --version
    

If you've followed these steps and the issue persists, consider creating a minimal example project to isolate the problem. This can help determine if the issue is with your specific setup or a broader problem.

Please or to participate in this conversation.