app/Providers/EventServiceProvider.php file and register our custom event and listener mappings.php artisan event:generateapp/Events/ClearCache.php class to give any property or methods (ex. public $cache_keys = [];)event helper with an insance of your custom eventapp/Providers/EventServiceProvider.phpnamespace App\Providers;
use Illuminate\Support\Facades\Event;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
'App\Events\Event' => [
'App\Listeners\EventListener',
],
];
/**
* Register any events for your application.
*
* @return void
*/
public function boot()
{
parent::boot();
//
}
}
Illuminate\Auth\Events\Login is an event that'll be raised when someone logs into an application. We'll bound that event to the App\Listeners\SendEmailNotification listener, so it'll be triggered on the login event.namespace App\Providers;
use Illuminate\Support\Facades\Event;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
'Illuminate\Auth\Events\Login' => [
'App\Listeners\SendEmailNotification',
],
];
/**
* Register any events for your application.
*
* @return void
*/
public function boot()
{
parent::boot();
//
}
}
php artisan event:generate
Illuminate\Auth\Events\Login event already exists, so it only creates the App\Listeners\SendEmailNotification listener class. In fact, it would have created the Illuminate\Auth\Events\Login event class too if it didn't exist.app/Listeners/SendEmailNotification.php.namespace App\Listeners;
use Illuminate\Auth\Events\Login;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class SendEmailNotification
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param Login $event
* @return void
*/
public function handle(Login $event)
{
}
}
handle method that will be invoked with appropriate dependencies whenever the listener is triggered.CacheClear event.app/Providers/EventServiceProvider.php file and register our custom event and listener mappings.namespace App\Providers;
use Illuminate\Support\Facades\Event;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
'App\Events\ClearCache' => [
'App\Listeners\WarmUpCache',
],
];
/**
* Register any events for your application.
*
* @return void
*/
public function boot()
{
parent::boot();
//
}
}
php artisan event:generate
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class ClearCache
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $cache_keys = [];
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Array $cache_keys)
{
$this->cache_keys = $cache_keys;
}
/**
* Get the channels the event should broadcast on.
*
* @return Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}
app/Listeners/WarmUpCache.php.namespace App\Listeners;
use App\Events\ClearCache;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class WarmUpCache
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param ClearCache $event
* @return void
*/
public function handle(ClearCache $event)
{
if (isset($event->cache_keys) && count($event->cache_keys)) {
foreach ($event->cache_keys as $cache_key) {
// generate cache for this key
// warm_up_cache($cache_key)
}
}
}
}
app/Http/Controllers/EventController.php to demonstrate how you could raise an event.namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Library\Services\Contracts\CustomServiceInterface;
use App\Post;
use Illuminate\Support\Facades\Gate;
use App\Events\ClearCache;
class EventController extends Controller
{
public function index()
{
// ...
// you clear specific caches at this stage
$arr_caches = ['categories', 'products'];
// want to raise ClearCache event
event(new ClearCache($arr_caches));
// ...
}
}
event helper function is used to raise an event from anywhere within an application.// app/Listeners/ExampleEventSubscriber.php
namespace App\Listeners;
class ExampleEventSubscriber
{
/**
* Handle user login events.
*/
public function sendEmailNotification($event) {
// get logged in username
$email = $event->user->email;
$username = $event->user->name;
// send email notification about login...
}
/**
* Handle user logout events.
*/
public function warmUpCache($event) {
if (isset($event->cache_keys) && count($event->cache_keys)) {
foreach ($event->cache_keys as $cache_key) {
// generate cache for this key
// warm_up_cache($cache_key)
}
}
}
/**
* Register the listeners for the subscriber.
*
* @param Illuminate\Events\Dispatcher $events
*/
public function subscribe($events)
{
$events->listen(
'Illuminate\Auth\Events\Login',
'App\Listeners\ExampleEventSubscriber@sendEmailNotification'
);
$events->listen(
'App\Events\ClearCache',
'App\Listeners\ExampleEventSubscriber@warmUpCache'
);
}
}
subscribe method that is responsible for registering listeners. The first argument of the subscribe method is the instance of the Illuminate\Events\Dispatcher class that you could use to bind events with listeners using the listen method.namespace App\Providers;
use Illuminate\Support\Facades\Event;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
/**
* The subscriber classes to register.
*
* @var array
*/
protected $subscribe = [
'App\Listeners\ExampleEventSubscriber',
];
/**
* Register any events for your application.
*
* @return void
*/
public function boot()
{
parent::boot();
//
}
}
Labels: Laravel, Web development