December 7, 2016 —John Koster
The event:generate
command is used to generate missing events and listeners based on the events that are registered in the applications EventServiceProvider
(by default this service provider is located in the app/Providers/EventServiceProvider.php
file).
Assuming the following EventServiceProvider
:
1<?php 2 3namespace App\Providers; 4 5use Illuminate\Contracts\Events\Dispatcher as DispatcherContract; 6use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; 7 8class EventServiceProvider extends ServiceProvider 9{10 /**11 * The event listener mappings for the application.12 *13 * @var array14 */15 protected $listen = [16 'App\Events\BookWasPurchased' => [17 'App\Listeners\EmailPurchaseConfirmation',18 ],19 ];20 21 /**22 * Register any other events for your application.23 *24 * @param \Illuminate\Contracts\Events\Dispatcher $events25 * @return void26 */27 public function boot(DispatcherContract $events)28 {29 parent::boot($events);30 31 //32 }33}
We can generate an Event
class for BookWasPurchased
and an EventListener
class for EmailPurchaseConfirmation
like so:
1# Generate missing events and listeners.2php artisan event:generate
The newly created event and listener will be stored in the following files, respectively: app/Events/BookWasPurchased.php
and app/Listeners/EmailPurchaseConfirmation.php
.
It should also be noted that this command will not overwrite any previously generated events or listeners and can be ran as many times as necessary.
The event:generate
command is simply a convenient wrapper around the make:listener
and make:event
commands.
The classes generated in the previous example can be manually generated like so:
1# Generate the BookWasPurchasedEvent event class.2php artisan make:event BookWasPurchasedEvent3 4# Generate the EmailPurchaseConfirmation listener class.5php artisan EmailPurchaseConfirmation --event=BookWasPurchasedEvent
∎