@@ -334,7 +334,9 @@ Dispatch the Event
334
334
..................
335
335
336
336
The :method: `Symfony\\ Component\\ EventDispatcher\\ EventDispatcher::dispatch `
337
- method notifies all listeners of the given event. It takes two arguments:
337
+ method notifies all listeners of the given event.
338
+
339
+ It takes two arguments:
338
340
the ``Event `` instance to pass to each listener of that event and the name
339
341
of the event to dispatch::
340
342
@@ -348,8 +350,14 @@ of the event to dispatch::
348
350
// creates the OrderPlacedEvent and dispatches it
349
351
$event = new OrderPlacedEvent($order);
350
352
$dispatcher->dispatch($event, OrderPlacedEvent::NAME);
351
- // note that the second argument ``OrderPlacedEvent::NAME`` is optional,
352
- // read more below in the subscriber code part
353
+
354
+ Note that the event name is now optional in the :method: `Symfony\\ Component\\ EventDispatcher\\ EventDispatcher::dispatch ` method,
355
+ so you can pass just the event object as first argument::
356
+
357
+ // ...
358
+ // creates the OrderPlacedEvent and dispatches it
359
+ $event = new OrderPlacedEvent($order);
360
+ $dispatcher->dispatch($event);
353
361
354
362
Notice that the special ``OrderPlacedEvent `` object is created and passed to
355
363
the ``dispatch() `` method. Now, any listener to the ``order.placed ``
@@ -392,10 +400,9 @@ Take the following example of a subscriber that subscribes to the
392
400
['onKernelResponsePre', 10],
393
401
['onKernelResponsePost', -10],
394
402
],
403
+ // when using two arguments, event and event name
395
404
OrderPlacedEvent::NAME => 'onStoreOrder',
396
- // you can also subscribe this way if you pass only
397
- // the event object as first argument and omit the second
398
- // of the $dispatcher->dispatch method
405
+ // when using only one argument, the event
399
406
OrderPlacedEvent::class => 'onStoreOrder',
400
407
];
401
408
}
0 commit comments