@@ -69,17 +69,6 @@ An :class:`Symfony\\Contracts\\EventDispatcher\\Event` instance is also
69
69
created and passed to all of the listeners. As you'll see later, the ``Event ``
70
70
object itself often contains data about the event being dispatched.
71
71
72
- Naming Conventions
73
- ..................
74
-
75
- The unique event name can be any string, but optionally follows a few
76
- naming conventions:
77
-
78
- * Use only lowercase letters, numbers, dots (``. ``) and underscores (``_ ``);
79
- * Prefix names with a namespace followed by a dot (e.g. ``order.* ``, ``user.* ``);
80
- * End names with a verb that indicates what action has been taken (e.g.
81
- ``order.placed ``).
82
-
83
72
Event Names and Event Objects
84
73
.............................
85
74
@@ -257,7 +246,7 @@ system flexible and decoupled.
257
246
Creating an Event Class
258
247
.......................
259
248
260
- Suppose you want to create a new event - `` order.placed `` - that is dispatched
249
+ Suppose you want to create a new event that is dispatched
261
250
each time a customer orders a product with your application. When dispatching
262
251
this event, you'll pass a custom event instance that has access to the placed
263
252
order. Start by creating this custom event class and documenting it::
@@ -268,17 +257,12 @@ order. Start by creating this custom event class and documenting it::
268
257
use Symfony\Contracts\EventDispatcher\Event;
269
258
270
259
/**
271
- * The order.placed event is dispatched each time an order is created
272
- * in the system.
260
+ * This event is dispatched each time an order
261
+ * is placed in the system.
273
262
*/
274
- class OrderPlacedEvent extends Event
263
+ final class OrderPlacedEvent extends Event
275
264
{
276
- public const NAME = 'order.placed';
277
-
278
- public function __construct(
279
- protected Order $order,
280
- ) {
281
- }
265
+ public function __construct(private Order $order) {}
282
266
283
267
public function getOrder(): Order
284
268
{
@@ -314,10 +298,10 @@ of the event to dispatch::
314
298
315
299
// creates the OrderPlacedEvent and dispatches it
316
300
$event = new OrderPlacedEvent($order);
317
- $dispatcher->dispatch($event, OrderPlacedEvent::NAME );
301
+ $dispatcher->dispatch($event);
318
302
319
303
Notice that the special ``OrderPlacedEvent `` object is created and passed to
320
- the ``dispatch() `` method. Now, any listener to the ``order.placed ``
304
+ the ``dispatch() `` method. Now, any listener to the ``OrderPlacedEvent::class ``
321
305
event will receive the ``OrderPlacedEvent ``.
322
306
323
307
.. _event_dispatcher-using-event-subscribers :
@@ -336,7 +320,7 @@ events it should subscribe to. It implements the
336
320
interface, which requires a single static method called
337
321
:method: `Symfony\\ Component\\ EventDispatcher\\ EventSubscriberInterface::getSubscribedEvents `.
338
322
Take the following example of a subscriber that subscribes to the
339
- ``kernel.response `` and ``order.placed `` events::
323
+ ``kernel.response `` and ``OrderPlacedEvent::class `` events::
340
324
341
325
namespace Acme\Store\Event;
342
326
@@ -354,7 +338,7 @@ Take the following example of a subscriber that subscribes to the
354
338
['onKernelResponsePre', 10],
355
339
['onKernelResponsePost', -10],
356
340
],
357
- OrderPlacedEvent::NAME => 'onStoreOrder ',
341
+ OrderPlacedEvent::class => 'onPlacedOrder ',
358
342
];
359
343
}
360
344
@@ -368,8 +352,9 @@ Take the following example of a subscriber that subscribes to the
368
352
// ...
369
353
}
370
354
371
- public function onStoreOrder (OrderPlacedEvent $event): void
355
+ public function onPlacedOrder (OrderPlacedEvent $event): void
372
356
{
357
+ $order = $event->getOrder();
373
358
// ...
374
359
}
375
360
}
@@ -413,14 +398,14 @@ inside a listener via the
413
398
414
399
use Acme\Store\Event\OrderPlacedEvent;
415
400
416
- public function onStoreOrder (OrderPlacedEvent $event): void
401
+ public function onPlacedOrder (OrderPlacedEvent $event): void
417
402
{
418
403
// ...
419
404
420
405
$event->stopPropagation();
421
406
}
422
407
423
- Now, any listeners to ``order.placed `` that have not yet been called will
408
+ Now, any listeners to ``OrderPlacedEvent::class `` that have not yet been called will
424
409
*not * be called.
425
410
426
411
It is possible to detect if an event was stopped by using the
0 commit comments