Playing
01. Automatically generating events and listeners in Laravel

Episodes

0%
Your progress
  • Total: 1m
  • Played: 0m
  • Remaining: 1m
Join or sign in to track your progress

Transcript

00:00
So this is just a really quick snippet which will hopefully save you huge amounts of time if you're working with Laravel events and listeners. So typically what we would do is inside of a project, we would use phpArtisan make. So if we just head up to here, we'd go ahead and make an event.
00:19
We would go ahead and make a listener probably providing the event in there so it can be type-hinted. And then we would head over to our app and go over to the event service provider and we'd register that event and the listener.
00:33
Now what you can actually do is if you provide your events and listeners as strings directly into your event service provider before you've gone ahead and created them, you can actually generate these events out. Now this isn't going to work if you pull in the actual class itself because that isn't
00:49
actually going to exist. Let's take a little example and I'll show you what I mean. So if we go ahead and say app events, let's just say order created as an example, I recently used this in the e-commerce course, then we would want to go ahead and empty the cart.
01:06
So we would inside of app and listeners define out empty cart. So as long as you define these out as a string, what you can actually do is and you can see that I've got no events or listener folder here is run phpArtisan event generate. And what that's going to do is it's going to look for any classes that don't exist in
01:25
here, e.g. these two, and it's going to automatically create them for you. So you can see that that has been created and that has been created. So that's pretty much it really. If you know which events and listeners you want, pop them in here.
01:38
You can run this as many times as you want even if they've already been created. They won't be overridden if they already exist. And hopefully this is going to save you a huge amount of time if you are working with events and listeners in Laravel.

Episode summary

In this quick episode, we look at an easy way to speed up your Laravel workflow when you're dealing with events and listeners. Normally, you'd use Artisan commands to manually create your events and listeners, and then wire them up in your EventServiceProvider. That works, but it can get a bit repetitive and tedious, especially if your app is growing.

Instead, this video shows a handy trick: you can define your events and listeners as strings in your EventServiceProvider—even before creating their actual files. Then, by running php artisan event:generate, Laravel will automatically create the event and listener classes for you if they don't already exist!

This means you can quickly scaffold out all the events and listeners you need just by specifying their names. And don't worry, if you've already created some of them, running the command again won't overwrite anything. It's a neat way to save time and keep your workflow smooth when setting up events in Laravel.

Episode discussion

No comments, yet. Be the first!