8000 minor #17685 [EventDispatcher] Allow to omit the event name when regi… · symfony/symfony-docs@6f3ff7e · GitHub
[go: up one dir, main page]

Skip to content

Commit 6f3ff7e

Browse files
committed
minor #17685 [EventDispatcher] Allow to omit the event name when registering listeners (alamirault)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [EventDispatcher] Allow to omit the event name when registering listeners Try complete #12451 I think best practice today is to not define `event` manually `@derrabus`, I will appreciate your review 🙏 Commits ------- 62f5165 [EventDispatcher] Allow to omit the event name when registering listeners
2 parents cc078fd + 62f5165 commit 6f3ff7e

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

event_dispatcher.rst

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ The most common way to listen to an event is to register an **event listener**::
3232

3333
class ExceptionListener
3434
{
35-
public function onKernelException(ExceptionEvent $event)
35+
public function __invoke(ExceptionEvent $event): void
3636
{
3737
// You get the exception object from the received event
3838
$exception = $event->getThrowable();
@@ -60,16 +60,8 @@ The most common way to listen to an event is to register an **event listener**::
6060
}
6161
}
6262

63-
.. tip::
64-
65-
Each event receives a slightly different type of ``$event`` object. For
66-
the ``kernel.exception`` event, it is :class:`Symfony\\Component\\HttpKernel\\Event\\ExceptionEvent`.
67-
Check out the :doc:`Symfony events reference </reference/events>` to see
68-
what type of object each event provides.
69-
7063
Now that the class is created, you need to register it as a service and
71-
notify Symfony that it is a "listener" on the ``kernel.exception`` event by
72-
using a special "tag":
64+
notify Symfony that it is a event listener by using a special "tag":
7365

7466
.. configuration-block::
7567

@@ -78,8 +70,7 @@ using a special "tag":
7870
# config/services.yaml
7971
services:
8072
App\EventListener\ExceptionListener:
81-
tags:
82-
- { name: kernel.event_listener, event: kernel.exception }
73+
tags: [kernel.event_listener]
8374
8475
.. code-block:: xml
8576
@@ -92,7 +83,7 @@ using a special "tag":
9283
9384
<services>
9485
<service id="App\EventListener\ExceptionListener">
95-
<tag name="kernel.event_listener" event="kernel.exception"/>
86+
<tag name="kernel.event_listener"/>
9687
</service>
9788
</services>
9889
</container>
@@ -108,7 +99,7 @@ using a special "tag":
10899
$services = $containerConfigurator->services();
109100
110101
$services->set(ExceptionListener::class)
111-
->tag('kernel.event_listener', ['event' => 'kernel.exception'])
102+
->tag('kernel.event_listener')
112103
;
113104
};
114105
@@ -117,10 +108,7 @@ listener class:
117108

118109
#. If the ``kernel.event_listener`` tag defines the ``method`` attribute, that's
119110
the name of the method to be called;
120-
#. If no ``method`` attribute is defined, try to call the method whose name
121-
is ``on`` + "PascalCased event name" (e.g. ``onKernelException()`` method for
122-
the ``kernel.exception`` event);
123-
#. If that method is not defined either, try to call the ``__invoke()`` magic
111+
#. If no ``method`` attribute is defined, try to call the ``__invoke()`` magic
124112
method (which makes event listeners invokable);
125113
#. If the ``__invoke()`` method is not defined either, throw an exception.
126114

@@ -134,6 +122,27 @@ listener class:
134122
internal Symfony listeners usually range from ``-256`` to ``256`` but your
135123
own listeners can use any positive or negative integer.
136124

125+
.. note::
126+
127+
There is an optional attribute for the ``kernel.event_listener`` tag called
128+
``event`` which is useful when listener ``$event`` argument is not typed.
129+
If you configure it, it will change type of ``$event`` object.
130+
For the ``kernel.exception`` event, it is :class:`Symfony\\Component\\HttpKernel\\Event\\ExceptionEvent`.
131+
Check out the :doc:`Symfony events reference </reference/events>` to see
132+
what type of object each event provides.
133+
134+
With this attribute, Symfony follows this logic to decide which method to call
135+
inside the event listener class:
136+
137+
#. If the ``kernel.event_listener`` tag defines the ``method`` attribute, that's
138+
the name of the method to be called;
139+
#. If no ``method`` attribute is defined, try to call the method whose name
140+
is ``on`` + "PascalCased event name" (e.g. ``onKernelException()`` method for
141+
the ``kernel.exception`` event);
142+
#. If that method is not defined either, try to call the ``__invoke()`` magic
143+
method (which makes event listeners invokable);
144+
#. If the ``__invoke()`` method is not defined either, throw an exception.
145+
137146
Defining Event Listeners with PHP Attributes
138147
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
139148

0 commit comments

Comments
 (0)
0