@@ -32,7 +32,7 @@ The most common way to listen to an event is to register an **event listener**::
32
32
33
33
class ExceptionListener
34
34
{
35
- public function onKernelException (ExceptionEvent $event)
35
+ public function __invoke (ExceptionEvent $event): void
36
36
{
37
37
// You get the exception object from the received event
38
38
$exception = $event->getThrowable();
@@ -60,16 +60,8 @@ The most common way to listen to an event is to register an **event listener**::
60
60
}
61
61
}
62
62
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
-
70
63
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":
73
65
74
66
.. configuration-block ::
75
67
@@ -78,8 +70,7 @@ using a special "tag":
78
70
# config/services.yaml
79
71
services :
80
72
App\EventListener\ExceptionListener :
81
- tags :
82
- - { name: kernel.event_listener, event: kernel.exception }
73
+ tags : [kernel.event_listener]
83
74
84
75
.. code-block :: xml
85
76
@@ -92,7 +83,7 @@ using a special "tag":
92
83
93
84
<services >
94
85
<service id =" App\EventListener\ExceptionListener" >
95
- <tag name =" kernel.event_listener" event = " kernel.exception " />
86
+ <tag name =" kernel.event_listener" />
96
87
</service >
97
88
</services >
98
89
</container >
@@ -108,7 +99,7 @@ using a special "tag":
108
99
$services = $containerConfigurator->services();
109
100
110
101
$services->set(ExceptionListener::class)
111
- ->tag('kernel.event_listener', ['event' => 'kernel.exception'] )
102
+ ->tag('kernel.event_listener')
112
103
;
113
104
};
114
105
@@ -117,10 +108,7 @@ listener class:
117
108
118
109
#. If the ``kernel.event_listener `` tag defines the ``method `` attribute, that's
119
110
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
124
112
method (which makes event listeners invokable);
125
113
#. If the ``__invoke() `` method is not defined either, throw an exception.
126
114
@@ -134,6 +122,27 @@ listener class:
134
122
internal Symfony listeners usually range from ``-256 `` to ``256 `` but your
135
123
own listeners can use any positive or negative integer.
136
124
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
+
137
146
Defining Event Listeners with PHP Attributes
138
147
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
139
148
0 commit comments