John Davidson

php - Symfony5 Listen custom event in bundle

0 comments
Message:


all


I would like to Listen an event in the custom bundle. I know that the problem is wiring an event because when I use it in the main path it works. My code is simple:


Firstly I have an event class like BeforeCrudActionEvent which has some default felds/methods.
Secodly I create instance of my event class and dispatch it using EventDispatcherInterface like that:


$event = new BeforeCrudActionEvent();
$this->get('event_dispatcher')->dispatch($event);

The name of event is not defined and it is generated automatically (consists namespace and name of the class).


Lastly I created Subscriber class which implements EventSubscriberInterface


class TestSubscriber implements EventSubscriberInterface
{

public static function getSubscribedEvents()
{
return [
BeforeCrudActionEvent::class => ['updateList']
];
}

public function updateList(BeforeCrudActionEvent $event)
{
throw new \Exception("It's works");
}
}

As I wrote before when the Subscriber class is in the main directory it works, but when it is in the custom bundle it doesn't. Im sure that it have to be added to service.yaml file but I can't find this configuration in the documentation. Which tag and which event (if required) schould I use?



My Answer:
eventDispatcher->dispatch($event);_And finally, I want to listen to this event in my custom bundle. I have created a listener class like BeforeCrudActionListener and added the necessary code to handle the event. However, I am not sure how to wire this listener to the event in my custom bundle. Can you please provide some guidance on how to do this?

Rate this post

4 of 5 based on 1083 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us