Skip to content

Commit 5bb7c53

Browse files
committed
minor #9281 Documented the new invokable event listeners (javiereguiluz)
This PR was squashed before being merged into the master branch (closes #9281). Discussion ---------- Documented the new invokable event listeners This fixes #9093. Commits ------- 4327f94 Documented the new invokable event listeners
2 parents acbf568 + 4327f94 commit 5bb7c53

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

event_dispatcher.rst

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,19 +107,31 @@ using a special "tag":
107107
->addTag('kernel.event_listener', array('event' => 'kernel.exception'))
108108
;
109109
110+
Symfony follows this logic to decide which method to execute inside the event
111+
listener class:
112+
113+
#. If the ``kernel.event_listener`` tag defines the ``method`` attribute, that's
114+
the name of the method to be executed;
115+
#. If no ``method`` attribute is defined, try to execute the method whose name
116+
is ``on`` + "camel-cased event name" (e.g. ``onKernelException()`` method for
117+
the ``kernel.exception`` event);
118+
#. If that method is not defined either, try to execute the ``__invoke()`` magic
119+
method (which makes event listeners invokable);
120+
#. If the ``_invoke()`` method is not defined either, throw an exception.
121+
122+
.. versionadded:: 4.1
123+
The support of the ``__invoke()`` method to create invokable event listeners
124+
was introduced in Symfony 4.1.
125+
110126
.. note::
111127

112-
There is an optional tag attribute called ``method`` which defines which method
113-
to execute when the event is triggered. By default the name of the method is
114-
``on`` + "camel-cased event name". If the event is ``kernel.exception`` the
115-
method executed by default is ``onKernelException()``.
116-
117-
The other optional tag attribute is called ``priority``, which defaults to
118-
``0`` and it controls the order in which listeners are executed (the highest
119-
the priority, the earlier a listener is executed). This is useful when you
120-
need to guarantee that one listener is executed before another. The priorities
121-
of the internal Symfony listeners usually range from ``-255`` to ``255`` but
122-
your own listeners can use any positive or negative integer.
128+
There is an optional attribute for the ``kernel.event_listener`` tag called
129+
``priority``, which defaults to ``0`` and it controls the order in which
130+
listeners are executed (the highest the priority, the earlier a listener is
131+
executed). This is useful when you need to guarantee that one listener is
132+
executed before another. The priorities of the internal Symfony listeners
133+
usually range from ``-255`` to ``255`` but your own listeners can use any
134+
positive or negative integer.
123135

124136
.. _events-subscriber:
125137

0 commit comments

Comments
 (0)