use Symfony\Component\EventDispatcher\Event; class MyEvent extends Event { public function __construct() { $this->name = 'my_event'; $this->url = 'https://example.com/my-event'; } public function getUrl(): string { return $this->url; } } $event = new MyEvent(); $url = $event->getUrl(); echo $url; // Output: https://example.com/my-event
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\Event; class MySubscriber implements EventSubscriberInterface { public static function getSubscribedEvents() { return [ 'my_event' => 'onMyEvent', ]; } public function onMyEvent(Event $event) { $url = $event->getUrl(); echo $url; // Output: https://example.com/my-event } } $event = new MyEvent(); $dispatcher = new EventDispatcher(); $dispatcher->addSubscriber(new MySubscriber()); $dispatcher->dispatch($event->name, $event);In this example, we define a custom event subscriber called `MySubscriber` that subscribes to the `my_event` event. The `onMyEvent` method is called when the event is triggered and accepts an instance of the `Event` class as its argument. Within the method, we retrieve the URL of the event using the `getUrl` method of the event instance. We then create an instance of the `MyEvent` class and pass it to the `dispatch` method of the `EventDispatcher` class, which triggers the `my_event` event and calls the `onMyEvent` method of the `MySubscriber` class. This method is a part of the Symfony\EventDispatcher\Event class in the Symfony EventDispatcher component.