From the course: Symfony 6 Essential Training
Unlock the full course today
Join today to access over 25,100 courses taught by industry experts.
Events - Symfony Tutorial
From the course: Symfony 6 Essential Training
Events
- [Narrator] A fairly common architecture in most frameworks is the event. During a typical request in Symfony, many events are fired. Let's take a look. Here's a simple controller that says hello to the username provided. It's just a route at hello and treats the next string as a name and in browser we see. Hi Gary! Let's add an event listener into the mix. I'll start by creating an event listener directory and add a new class, DemoListener. Above the class, I'll add the attribute. As event listener, make sure to import it AsEventListener. My IDE auto-completed it for me. Events can run a few ways. I find it easiest to use the invoke method. So, public function invoke and I'm type hinting on response event. To show it's working, I'm just going to add a dump and die. I'll get the event, I'll get the response, and I'll get the content and we see it's working. So, what can you do with it? Well, let's update this logic…