In the first part of our article, we explored how to create a custom PHP attribute and a listener factory for registering event listeners in Laravel. By using the ListensTo attribute, we can define which events our listener classes are interested in without cluttering the listener code with boilerplate. Now, let’s dive into creating event listeners and registering them using this attribute.
Creating Events and Listeners
Creating events and listeners in Laravel is as simple as running the two commands below:
php artisan make:events EventName
php artisan make:listener ListenerName
Defining Event Listeners
Now, we will create event listener classes that utilise the ListensTo attribute to specify the events they listen to. Here’s an example of two listeners:
1. SendWelcomeMail Listener
<?php
namespace App\Listeners;
use App\Support\Attributes\ListensTo;
use Illuminate\Auth\Events\Registered;
#[ListensTo(Registered::class)] // Attribute is bound here as a string
class SendWelcomeMail
{
public function handle(object $event): void
{
//
}
}
2. SendAccountCreated Listener
<?php
namespace App\Listeners;
use App\Events\SignUpConfirmed;
use App\Support\Attributes\ListensTo;
use Illuminate\Auth\Events\Registered;
#[ListensTo([SignUpConfirmed::class, Registered::class])] // Attribute is bound here as a array
class SendAccountCreated
{
public function handle(object $event): void
{
//
}
}
Explanation of Listener Classes
Each listener class:
Uses the #[ListensTo] attribute to declare which events it listens for.
Contains a handle method, which Laravel will call when the specified event is fired.
Integrating with Laravel
Bootstrapping Event Listeners
To ensure that our listeners are registered when the application starts, we need to integrate the ListenerFactory in the boot method of AppServiceProvider.
public function boot(): void
{
// Bootstrap events and listeners
(new ListenerFactory)->register();
}
Explanation of Integration
In the AppServiceProvider, we call the register method of the ListenerFactory during the boot phase of the application. This ensures that all listeners are registered correctly before handling any events.
Benefits of Using Attributes in Laravel
Cleaner Code: Attributes reduce the boilerplate code often associated with going to AppServiceProvider to register new Listeners to their events, making the code cleaner and easier to understand.Plus less jumping between files.
Easy configuration: with this set up all you need to connect a listener to an event is to
php artisan make:listener Listenername
, add theListensTo
attribute and it’s registered.Modular Design: With attributes, listeners can be defined in a more modular way, making it easier to manage dependencies and event relationships.
Conclusion
In this two-part article, we’ve explored how to use PHP attributes to efficiently organise event listeners configuration in Laravel applications.
The ListensTo attribute allows us to define event listeners in a way that minimises boilerplate and enhances configuration. By adopting this approach, developers can create cleaner, more maintainable codebases.
I encourage you to explore using PHP attributes in your applications to take advantage of this powerful feature. Happy coding!