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.
1 episode 1 min

Overview

A quick tip that'll save you huge amounts of time if your app is heavily event driven. Define Laravel events and listeners, then have the classes automatically generated for you.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!