@arnoldo.moen
To create a new event in Symfony, you can follow these steps:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
namespace AppEvent; use SymfonyComponentEventDispatcherEvent; class NewEvent extends Event { const NAME = 'new.event'; private $data; public function __construct($data) { $this->data = $data; } public function getData() { return $this->data; } } |
1 2 3 4 |
services: AppEventListenerMyListener: tags: - { name: kernel.event_listener, event: 'new.event', method: onNewEvent } |
1 2 |
$event = new NewEvent($data); $dispatcher->dispatch(NewEvent::NAME, $event); |
1 2 3 4 5 6 7 8 9 10 11 12 |
namespace AppEventListener; use AppEventNewEvent; class MyListener { public function onNewEvent(NewEvent $event) { // Do something with the event data $data = $event->getData(); } } |
By following these steps, you can create a new event in Symfony and handle it using event listeners.
@arnoldo.moen
To create a new event in Symfony, you can follow these steps:
For example, let's say you want to create an event that is triggered when a user logs in. You could create a UserLoggedInEvent
class like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
namespace AppEvent; use SymfonyContractsEventDispatcherEvent; class UserLoggedInEvent extends Event { private $user; public function __construct($user) { $this->user = $user; } public function getUser() { return $this->user; } } |
For example, let's say you want to dispatch the UserLoggedInEvent
event when a user logs in. You could do this in your login controller like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
namespace AppController; use AppEventUserLoggedInEvent; use SymfonyBundleFrameworkBundleControllerAbstractController; use SymfonyComponentHttpFoundationRequest; use SymfonyComponentRoutingAnnotationRoute; use SymfonyContractsEventDispatcherEventDispatcherInterface; class LoginController extends AbstractController { /** * @Route("/login", name="login") */ public function login(Request $request, EventDispatcherInterface $eventDispatcher) { // Handle login logic... // Dispatch the UserLoggedInEvent event $user = ...; // get the logged-in user $event = new UserLoggedInEvent($user); $eventDispatcher->dispatch($event); // Redirect to the homepage return $this->redirectToRoute('homepage'); } } |
For example, let's say you want to send an email notification when a user logs in. You could create a UserLoggedInSubscriber
class like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
namespace AppEventSubscriber; use AppEventUserLoggedInEvent; use SymfonyComponentEventDispatcherEventSubscriberInterface; class UserLoggedInSubscriber implements EventSubscriberInterface { public static function getSubscribedEvents() { return [ UserLoggedInEvent::class => 'onUserLoggedIn', ]; } public function onUserLoggedIn(UserLoggedInEvent $event) { // Send email notification to the user $user = $event->getUser(); $message = ...; // create email message $mailer = ...; // get mailer service $mailer->send($message); } } |
To register this event subscriber, you can add the following to your services.yaml
file:
1 2 3 4 |
services: AppEventSubscriberUserLoggedInSubscriber: tags: - { name: kernel.event_subscriber } |
This will register the UserLoggedInSubscriber
class as a service and tag it as a kernel event subscriber, which will cause Symfony to automatically register it as a listener for the UserLoggedInEvent
event.