Eventing Overview 0:00Let's continue our training and we'll take a look at eventing. So I'm going to go to my event service provider. We haven't reviewed this one yet. So let's take a look. We have this array for listen, listen for some event to occur, and then we're going to trigger this listener. But we can see right now that these don't exist. We don't even have an events or listeners folder. Well, as it turns out, these are going to be generated on demand for you. So I want you to think of an event as a particular thing in your application of significance that took place. So maybe it could be as simple as a user registered, or maybe it could be that an order was processed, or a post was archived, or a form thread was created. These are significant actions that have taken place in the past that you are now alerting the rest of your application to. Think of situations where you say, well, when this happens,created. These are significant actions that have taken place in the past that you are now alerting the rest of your application to. Think of situations where you say, well, when this happens, I need to do A, B, and C. Well, to some extent, you can do that all inline. But what you may find is that the more you do it inline, the more gross, to be frank, your code gets because you just have lines, and lines, and lines, and all these various dependencies. So instead, by using eventing, we can simply raise an event. I think of that as holding up a bullhorn and yelling what just took place. And because you've yelled it loud enough, anywhere else in your application can now respond to that event that took place. Take a look. If I run phpArtisan, you'll see that, like the others, we have some generators. So I can make an event class, and I can make a listener. Now, an event class is a simple, plain old PHP object. So if we were to generate one here, make an event, Generating Events and Listeners 1:28we have some generators. So I can make an event class, and I can make a listener. Now, an event class is a simple, plain old PHP object. So if we were to generate one here, make an event, and let's just follow this, a sum event, and then we'll use a more real-world example in just a few minutes. Anyways, when we run that, you'll see that you get this new class right here. Now, here, within the constructor, is where we can accept whatever is necessary for this particular event. Maybe foo, and then we can assign it up here. Okay. Next, what about a listener class? A listener is one or more classes that want to respond to the event that just took place. So let's take a look at the help for this, and we can see we give it the name of the listener, but we can also pass the dash e flag to specify the associated event. So let's try it. phpArtisan make a listener, some kind of class to send a notification, perhaps, and now the event that we are listening forthe dash e flag to specify the associated event. So let's try it. phpArtisan make a listener, some kind of class to send a notification, perhaps, and now the event that we are listening for is sumEvent. Okay, take a look. Now we have a new listeners folder, as well as our class. If we scroll down, we can now handle the event that took place. So this is what I mean, where you can inject whatever is necessary, and then you can assign it to the instance, like this, public foo. Now, all of the listeners that are bound to some event can respond to that. So I can access event foo, like so. Now, you can do anything within here. You can send an email, you can perform a database query, you can notify your users in some way. Anything you want is fine. But now, the important thing to understand is that creating an event in a listener class alone won't do the trick. They have to be registered within your event service provider. Registering and Auto-Generating 3:03But now, the important thing to understand is that creating an event in a listener class alone won't do the trick. They have to be registered within your event service provider. So here we can see the sample was provided for us. When this event is fired, I want you to synchronously, at least by default, synchronously trigger these event listeners. But yeah, now we're starting to see it is a bit annoying to create an event and then create another listener. So what you can do is define your events and your listeners here, and then you'll see that we have, if we run this one more time, if we search for event, there is an event generate command. So what that's going to do is read this file, fetch this array, filter through them, and build any of the files that haven't already been created. So if I say phpArtisan event generate, you're going to get the exact same thing that you had before, but yeah, it's a little less cumbersome to do so. And further, when you do Thread Created Example 3:47haven't already been created. So if I say phpArtisan event generate, you're going to get the exact same thing that you had before, but yeah, it's a little less cumbersome to do so. And further, when you do it in this way, your listener class will also specify the event. Okay, let's use a more practical real-life example. Imagine that your application has a forum and you can create a thread. And as part of that, maybe a number of things need to take place. Thread subscribers need to be notified. A tweet needs to be published. Maybe you need to publish to Facebook as well. Lots of little things. So you're going to create an event for this. So why don't we say thread created. And next, we're going to have a listener called notify subscribers. So whenever a thread is published or created, we are now going to find any subscribers of our forum and notify them that a new thread has been published. Okay, this looks good to me. Of course, you can namespace these however you want. So ifwe are now going to find any subscribers of our forum and notify them that a new thread has been published. Okay, this looks good to me. Of course, you can namespace these however you want. So if this is related to your forum, then no problem. Let's just keep it simple though. So I will now run event generate. Oh, and actually, real quick on that note, if you use Sublime, I recommend installing a package called Laravel 5 Artisan. This will allow you to trigger any of these commands directly in the editor. So for example, you could say Artisan event generate, and now that runs for you. It just saves you a step. Anyways though, if we switch back, now we have our listener as well as the event. So our listener will now handle when a thread has been created. So now, what would we give to a thread created instance? Well, potentially you would give it the form thread. Something like this. And let's move this below. Okay, so now when we want to dispatch an event that a thread was Dispatching Events and Multiple Listeners 5:16give to a thread created instance? Well, potentially you would give it the form thread. Something like this. And let's move this below. Okay, so now when we want to dispatch an event that a thread was created, we will pass that through the constructor. And now any listener may access that thread by saying, once again, event thread. So maybe you get subscribers and you, for each one, you need to perform some kind of action. Any of that stuff is a good place for this. Anything like that would be a good fit here. For now, let's simply var dub event thread name was published to the form, just so we can see this in effect. So now, how do we actually dispatch the event? We've created the event class itself, we've created listeners, but how do we dispatch the event? We can do that via the helper event function. I can say fire off an event, and I'm going to give it the name of this class here. So that will be new app events thread created. But now, don't forget, a threadthe helper event function. I can say fire off an event, and I'm going to give it the name of this class here. So that will be new app events thread created. But now, don't forget, a thread created requires the thread, and that would probably be an eloquent model. But I'm just going to give it an array here. And that's it. So we run it, and sure enough, you can see that our listener caught that event and dumped it to the console. So now, imagine that we need to do something else. We've notified the subscribers, but maybe we also want to check for spam. Presumably, you might check for spam before it's published, but nonetheless, just as an example, we could say make a listener called check for spam, and the event that corresponds to that, once again, is thread created. Okay. Now we have another listener, and then once again, we could just var dump checking for spam. Okay. So now, we're going to update our service provider to specifyis thread created. Okay. Now we have another listener, and then once again, we could just var dump checking for spam. Okay. So now, we're going to update our service provider to specify that when a thread is created, yes, we want to notify the subscribers, but we also want to check for spam and potentially delete it after the fact. I don't know. Anyways, let's give it another shot. PHP Artisan Tinker. We're going to fire a thread created event. All of those listeners will now pick it up, and you can see that each one had the chance to respond. Now, in closing, this can be incredibly useful. My advice to you would be don't immediately reach for this for every possible action. Sometimes, sending the email directly through the controller using the mail send method that we learned about is absolutely the way you want to go. You just want to reach for this when you begin to realize that many different things can and will take place in response to this event. Queueing Event Listeners 7:47that we learned about is absolutely the way you want to go. You just want to reach for this when you begin to realize that many different things can and will take place in response to this event. In those situations, yes, absolutely fire the event. Oh, and also, one last quick thing before we close up shop. If we click on any listener, you'll see that we have a should queue interface, which you can see right here. That means if you want this event listener to be sent through a queue and, in effect, be treated as an asynchronous operation that doesn't hold up the initial request, and usually for firing off emails or doing time-sensitive things, you will want to do this. You can specify that this should be sent through a queue. The example I always use for this is imagine you go to a store and you order something that's in the factory in the very back. Well, the cashier could tell you to wait and then go all the way back to the factory,imagine you go to a store and you order something that's in the factory in the very back. Well, the cashier could tell you to wait and then go all the way back to the factory, fetch the item, get the ladder out, get it, pull it down, and then bring it back to you, but that's going to take a long time, right? Or, alternatively, the cashier could just send a little flyer to one of her co-workers, and then her co-worker will take care of that for you. So as a result, she can return right back to you where you don't have to wait, and she can continue finishing up your order. That's how I think of a queue. So that's definitely an option for you. All right, that'll do it for this episode.