Skip to content

Instantly share code, notes, and snippets.

@owenconti
Created May 19, 2020 01:32
Show Gist options
  • Select an option

  • Save owenconti/8fc1a6afe2602d52710021001ad06a92 to your computer and use it in GitHub Desktop.

Select an option

Save owenconti/8fc1a6afe2602d52710021001ad06a92 to your computer and use it in GitHub Desktop.
How to handle multiple events with a single listener in Laravel
<?php
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
LoginEvent::class => [
HandleLoginListener::class
],
];
}
// App\Events\NotifiableEvent.php
<?php
namespace App\Events;
interface NotifiableEvent
{
/**
* Returns the display name of the event.
*
* @return string
*/
public function getEventName(): string;
/**
* Returns the description of the event.
*
* @return string
*/
public function getEventDescription(): string;
}
// App\Events\CreatedApplicationMember.php
<?php
namespace App\Events;
class CreatedApplicationMember implements NotifiableEvent
{
public function getEventName(): string
{
return 'Created Application Member';
}
public function getEventDescription(): string
{
return 'Fired whenever a new Application Member is added to your Application.';
}
// constructor and stuff goes here...
<?php
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
NotifiableEvent::class => [
SendEventNotification::class
],
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment