DEV Community

Cover image for Using Events in Laravel
Kenta Takeuchi
Kenta Takeuchi

Posted on • Originally published at bmf-tech.com

Using Events in Laravel

This article was originally published on bmf-tech.com.

Managing methods you want to trigger during specific events like user registration or withdrawal is convenient with event listeners.

This time, we will skip the basic definition of events and listeners and focus on event subscription, which allows you to set multiple events in a single listener class.

Environment

  • laravel5.2

Directory

  • app\Events: Place classes where the event name equals the class name (no strict naming conventions)
  • app\Listeners: Place classes that implement processing (listeners) for each event and the subscribe method (described later)
  • app\Providers: Place classes that register listeners used in event subscriptions
  • app\Controllers: Prepare controllers and methods that call events

Define an Event

First, let's define an event. As an example, let's create an event called "User Registration Complete." You can generate an event class using the artisan command.

php artisan make:event UserRegistrationComplete


<?php

namespace App\Events;

use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class UserRegistrationComplete extends Event
{
    use SerializesModels;

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

    /**
     * Get the channels the event should be broadcast on.
     *
     * @return array
     */
    public function broadcastOn()
    {
        return [];
    }
}

Enter fullscreen mode Exit fullscreen mode

When you run the artisan command, a class like this is automatically generated. Set the data to be used in the event in the constructor.

Since this event is related to user registration, let's call the User model. The data called here can be used in the listener class.


    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(User $user)
    {
        $this->user =  $user;
    }

Enter fullscreen mode Exit fullscreen mode

By the way, broadcastOn is used when you want to implement a user interface that runs in real-time, so we will skip it this time.

Define a Listener

Next, let's define a listener. You can also generate a listener using the artisan command.

php artisan make:listener UserAuthEventListenerListener --event UserRegistrationComplete

When generating a listener, you can set the event you want to associate with using the event option.

<?php

namespace App\Listeners;

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

class UserAuthEventListenerListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  UserRegistrationComplete  $event
     * @return void
     */
    public function handle(UserRegistrationComplete $event)
    {
        //
    }
}
Enter fullscreen mode Exit fullscreen mode

Event subscription allows you to set multiple events in a single listener class, so it is good to name the listener like a category name for multiple events.

In this example, "User Registration Complete Event belongs to the User Authentication group."

In the generated listener, perform event firing processing and event subscription registration.

<?php

namespace App\Listeners;

class UserAuthEventListener
{
    // Event firing processing
    public function onConfirm($event)
    {
      // Processing
    }

    // You can add multiple
    public function onHogeHoge($event)
    {
      // Processing
    }

    // Event subscription registration
    public function subscribe($events)
    {
        $events->listen(
            'App\Events\UserRegistrationComplete',
            'App\Listeners\UserAuthEventListener@onConfirm'
        );
    }

    // You can register multiple
    public function subscribe($events)
    {
        $events->listen(
            'App\Events\UserHogeHoge',
            'App\Listeners\UserAuthEventListener@onHogeHoge'
        );
    }
}

Enter fullscreen mode Exit fullscreen mode

If you want to add an event, you can generate it with the artisan command as before.

Register Event Subscription Class in EventServiceProvider

It might seem complicated with all the registrations, but this is the last step. Use the EventServiceProvider that exists by default in app\Providers.

A service provider is a class that performs the initial startup process of an application. For more details, see the documentation

<?php

namespace App\Providers;

use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider
{
    // Register listeners here
    protected $subscribe = [
        'App\Listeners\UserAuthEventListener',
        'App\Listeners\HogeHogeListener',
    ];
}

Enter fullscreen mode Exit fullscreen mode

Fire

Now that the event subscription registration is complete, let's freely fire events.

To fire an event, use the Event facade and the fire method.

Pass the event instance to the fire method.

    /**
     * A certain method in the controller
     */
    private function hogehoge(User $user)
    {
        // Fire registration confirmation event
        \Event::fire(new UserAuthRegistrationComplete($user));
    }

Enter fullscreen mode Exit fullscreen mode

This is how the registered event ignites 🔥

Summary

Event definition (data retention) → Listener definition (method management used in events and subscribe method implementation) → Event subscription registration → 🔥

Additional Notes

Since Laravel 5.3, a package called Notification has been introduced, so managing notifications like this might be easier with Notification. Perhaps for this reason, directories like Event, Listener, and Job no longer exist by default in 5.3. If you're interested, check out the Laravel repository or documentation.

#References

Top comments (0)