Looking to hire Laravel developers? Try LaraJobs

laravel-listener-attributes maintained by imbrett

Description
Attribute-driven event listener registration for Laravel.
Last update
2026/11/06 04:21 (dev-main)
License
Downloads
3

Comments
comments powered by Disqus

Listener Attributes

Tiny helper that lets you register event listeners in Laravel using PHP attributes instead of manual service provider wiring.

Features

  • #[ListensTo(Event::class)] attribute for listener methods.
  • Service provider scans configured subscriber classes and registers them with the event dispatcher.
  • Works with container-resolved listeners and repeatable attributes.

Installation

composer require imbrett/laravel-listener-attributes

Usage

  1. Configure your subscriber classes (e.g. in config/listener-attributes.php):
return [
    'subscribers' => [
        \App\Listeners\AuthSubscriber::class,
    ],
];
  1. Annotate listener methods:
use ImBrett\LaravelListenerAttributes\Attributes\ListensTo;

class AuthSubscriber
{
    #[ListensTo(UserLoggedIn::class)]
    public function onLogin(UserLoggedIn $event): void
    {
        // ...
    }
}

The service provider will discover those methods at boot and call $dispatcher->listen() for you. Listeners are registered using the standard [$class, $method] signature so they benefit from container resolution on every dispatch.

Configuration

Publish and edit the default configuration:

php artisan vendor:publish --tag=listener-attributes-config

Testing

make test TEST_ARGS="--testsuite=ListenerAttributes"

Documentation

  • docs/index.md – documentation table of contents.
  • docs/getting-started.md – configuration and attribute usage.
  • docs/advanced.md – repeatable attributes, conditional registration, and queueing.
  • docs/testing.md – asserting registrations and debugging.
  • Register custom attributes at runtime via the AttributeRegistry or list them under listener-attributes.attributes.

License

MIT © Brett