Skip to content

AsEventListener Example Might Never Work #21002

Open
@miqrogroove

Description

@miqrogroove

I found a severe pitfall in this example code:

:class:`Symfony\\Component\\EventDispatcher\\Attribute\\AsEventListener`
can also be applied to methods directly::
namespace App\EventListener;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
final class MyMultiListener
{
#[AsEventListener]
public function onCustomEvent(CustomEvent $event): void

There seems to be an implicit assumption here that class CustomEvent has been defined and exists in the same namespace.

If CustomEvent is defined in the App\Event namespace used by other examples in this file then the AsEventListener attribute fails silently and the listener method will never do anything.

Specifically, there is a missing use statement here and it's not easy to debug.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @miqrogroove

        Issue actions

          AsEventListener Example Might Never Work · Issue #21002 · symfony/symfony-docs