Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 90d6742

Browse files
Update event_dispatcher.rst
1 parent 7d9e6fe commit 90d6742

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

components/event_dispatcher.rst

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,6 @@ of the event to dispatch::
348348
// creates the OrderPlacedEvent and dispatches it
349349
$event = new OrderPlacedEvent($order);
350350
$dispatcher->dispatch($event, OrderPlacedEvent::NAME);
351-
// note that the second argument ``OrderPlacedEvent::NAME`` is optional,
352-
// read more below in the subscriber code part
353351

354352
Notice that the special ``OrderPlacedEvent`` object is created and passed to
355353
the ``dispatch()`` method. Now, any listener to the ``order.placed``
@@ -392,10 +390,9 @@ Take the following example of a subscriber that subscribes to the
392390
['onKernelResponsePre', 10],
393391
['onKernelResponsePost', -10],
394392
],
393+
// when using two arguments, the event and the event name
395394
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
395+
// when using only one argument, the event (Symfony 4.3+)
399396
OrderPlacedEvent::class => 'onStoreOrder',
400397
];
401398
}
@@ -416,6 +413,16 @@ Take the following example of a subscriber that subscribes to the
416413
}
417414
}
418415

416+
.. versionadded:: 4.3
417+
418+
Since Symfony 4.3, note that the event name is now optional in the :method:`Symfony\\Component\\EventDispatcher\\EventDispatcher::dispatch` method,
419+
so you can pass just the event object as first argument::
420+
421+
// ...
422+
// creates the OrderPlacedEvent and dispatches it
423+
$event = new OrderPlacedEvent($order);
424+
$dispatcher->dispatch($event);
425+
419426
This is very similar to a listener class, except that the class itself can
420427
tell the dispatcher which events it should listen to. To register a subscriber
421428
with the dispatcher, use the

0 commit comments

Comments
 (0)