Laravel Artisan Generator Command: The make:listener Command

December 7, 2016 —John Koster

The make:listener command is used to generate a new event listener class. A name must be supplied to the make:listener command; this value will be the name of the newly generated class. The command also defines one required option: event. The event option is used to specify which event class the listener is being generated for. A queued flag can also be specified to indicate that the listener should be queued.

The following would generate a listener class for the OrderWasCanceled event class (see the section for the make:event command):

1# Generate a listener class for the OrderWasCanceled event class.
2php artisan make:listener EmailOrderCanceledConfirmation --event=OrderWasCanceled

After the above command has been executed a new file will be created at app/Listeners/EmailOrderCanceledConfirmation.php. The file will contain a new class named EmailOrderCanceledConfirmation.

This command does not update the $listen mappings in the app/Providers/EventServiceProvider.php service provider.

When the --queued flag is specified, the newly created class will implement the Illuminate\Contracts\Queue\ShouldQueue interface and make use of the Illuminate\Queue\InteractsWithQueue trait to provide the actual implementation.

The following example would generate the same listener class as before, but with the --queued flag supplied:

1php artisan make:listener EmailOrderCanceledConfirmation --event=OrderWasCanceled --queued

The following PHP snippet shows that the newly generated class contains the previously mentioned interface and trait:

1<?php
2 
3namespace App\Listeners;
4 
5use App\Events\OrderWasCanceled;
6use Illuminate\Queue\InteractsWithQueue;
7use Illuminate\Contracts\Queue\ShouldQueue;
8 
9class EmailOrderCanceledConfirmation2 implements ShouldQueue
10{
11 use InteractsWithQueue;
12 
13 
14 // Rest of class omitted.
15}

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.