php
44 lines · 6 steps
How event subscribers group listeners in Laravel
A single subscriber class handles multiple order events by mapping each event to a handler method.
Explained by
highlit
1<?php
2
3namespace App\Listeners;
4
5use App\Events\OrderPlaced;
6use App\Events\OrderRefunded;
7use App\Jobs\GenerateInvoice;
8use App\Notifications\OrderConfirmation;
9use Illuminate\Events\Dispatcher;
10use Illuminate\Support\Facades\Log;
11
12class OrderEventSubscriber
13{
14 public function handleOrderPlaced(OrderPlaced $event): void
15 {
16 $order = $event->order;
17
18 $order->customer->notify(new OrderConfirmation($order));
19 GenerateInvoice::dispatch($order)->onQueue('billing');
20
21 $order->warehouse->reserveStock($order->lineItems);
22 }
23
24 public function handleOrderRefunded(OrderRefunded $event): void
25 {
26 $order = $event->order;
27
28 $order->payment->issueRefund($event->amount);
29 $order->warehouse->releaseStock($order->lineItems);
30
31 Log::channel('finance')->info('Order refunded', [
32 'order_id' => $order->id,
33 'amount' => $event->amount->format(),
34 ]);
35 }
36
37 public function subscribe(Dispatcher $events): array
38 {
39 return [
40 OrderPlaced::class => 'handleOrderPlaced',
41 OrderRefunded::class => 'handleOrderRefunded',
42 ];
43 }
44}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1A subscriber class consolidates related event handlers so one bounded context lives in one place.
- 2Dispatching jobs to a named queue keeps slow side effects off the request path.
- 3The subscribe method's array is the contract that wires events to methods, keeping registration declarative.
Related explainers
php
<?php namespace App\Services;
How user impersonation works in Laravel
authentication
authorization
session
Intermediate
8 steps
php
class OrderReceiptController extends Controller { public function store(Request $request, Order $order) {
Handling receipt uploads in a Laravel controller
file-upload
validation
authorization
Intermediate
5 steps
php
<?php final class RememberMeCookie {
Signed remember-me cookies in PHP
authentication
hmac
cookies
Intermediate
8 steps
php
<?php namespace App\Http\Middleware;
Caching HTTP responses with Laravel middleware
middleware
caching
http
Intermediate
8 steps
php
<?php namespace App\Validation;
Building a reusable address form validator in PHP
validation
error-accumulation
regex
Intermediate
9 steps
php
<?php namespace App\Http\Controllers;
A cached autocomplete endpoint in Laravel
caching
validation
query-ranking
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/how-event-subscribers-group-listeners-in-laravel-explained-php-b068/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.