Sunday, September 13, 2015

/** * The event handler mappings for the application. * * @var array */ protected $listen = [ 'App\Events\PodcastWasPurchased' => [ 'App\Handlers\Events\EmailPurchaseConfirmation', ], ]; To generate a handler for an event, use the handler:event Artisan CLI command: php artisan handler:event EmailPurchaseConfirmation --event=PodcastWasPurchased Of course, manually running the make:event and handler:event commands each time you need a handler or event is cumbersome. Instead, simply add handlers and events to your EventServiceProvider and use the event:generate command. This command will generate any events or handlers that are listed in your EventServiceProvider: php artisan event:generate Firing An Event Now we are ready to fire our event using the Event facade: $response = Event::fire(new PodcastWasPurchased($podcast)); The fire method returns an array of responses that you can use to control what happens next in your application. You may also use the event helper to fire an event: event(new PodcastWasPurchased($podcast)); Closure Listeners You can even listen to events without creating a separate handler class at all. For example, in the boot method of your EventServiceProvider, you could do the following: Event::listen('App\Events\PodcastWasPurchased', function($event) { // Handle the event... });

No comments:

Post a Comment