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

Skip to content

Documented the new invokable event listeners #9281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions event_dispatcher.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,31 @@ using a special "tag":
->addTag('kernel.event_listener', array('event' => 'kernel.exception'))
;

Symfony follows this logic to decide which method to execute inside the event
listener class:

#. If the ``kernel.event_listener`` tag defines the ``method`` attribute, that's
the name of the method to be executed;
#. If no ``method`` attribute is defined, try to execute the method whose name
is ``on`` + "camel-cased event name" (e.g. ``onKernelException()`` method for
the ``kernel.exception`` event);
#. If that method is not defined either, try to execute the ``__invoke()`` magic
method (which makes event listeners invokable);
#. If the ``_invoke()`` method is not defined either, throw an exception.

.. versionadded:: 4.1
The support of the ``__invoke()`` method to create invokable event listeners
was introduced in Symfony 4.1.

.. note::

There is an optional tag attribute called ``method`` which defines which method
to execute when the event is triggered. By default the name of the method is
``on`` + "camel-cased event name". If the event is ``kernel.exception`` the
method executed by default is ``onKernelException()``.

The other optional tag attribute is called ``priority``, which defaults to
``0`` and it controls the order in which listeners are executed (the highest
the priority, the earlier a listener is executed). This is useful when you
need to guarantee that one listener is executed before another. The priorities
of the internal Symfony listeners usually range from ``-255`` to ``255`` but
your own listeners can use any positive or negative integer.
There is an optional attribute for the ``kernel.event_listener`` tag called
``priority``, which defaults to ``0`` and it controls the order in which
listeners are executed (the highest the priority, the earlier a listener is
executed). This is useful when you need to guarantee that one listener is
executed before another. The priorities of the internal Symfony listeners
usually range from ``-255`` to ``255`` but your own listeners can use any
positive or negative integer.

.. _events-subscriber:

Expand Down