This episode is for members only

Sign up to access "Laravel Actions" right now.

Get started
Already a member? Sign in to continue
Playing
08. Actions as event listeners

Transcript

00:00
We're now going to update this create order action to dispatch an event to say that the order was created. And what we can then do is hook into this and maybe send an email to the user to tell them that the order was created. Now, the reason that we're not going to go ahead and send an email within this is that we want these to have a single responsibility.
00:23
We want them to just do one thing. If we were to tie this specific action into sending the email as well, it makes it a little bit more difficult and a little bit more inflexible. So what we're going to do in here is once we have created the order, we're going to go ahead and dispatch an event in here to say that the order was created. So the first thing that we need is the event itself.
00:47
So let's go ahead and make an event and we're going to call this order created. OK, so let's go down here and say event new order created. And we're going to pass the order itself into there, which we can grab from assigning what we get back from here, which is, of course, not much at the moment. OK, so we've got our order created event over here.
01:11
We don't really need any of this broadcast stuff unless you did actually want to broadcast this. So I'm going to go ahead and get rid of that just so we can kind of focus on what we're doing here. And we're going to go ahead and set the order itself as a public property inside of here, just making sure we pull the namespace in for this. OK, so we've got our event.
01:32
Now, what we would typically do is just create a listener within Laravel. So what we would do is come over to the event service provider. We would come down here. We've already got this registered event in here. And we would say, well, when this order is created, what do we want to do?
01:49
Well, we want to go ahead and send an order email or send an order confirmation email. So what we would typically do now that we've set this up is go ahead and create out a listener for this. So let's just comment this out and go ahead and say PHP artisan make listener or we could use event generate to automatically create these for us. And we're going to call this send order email.
02:13
So we've got our listener. So we can go ahead and just pull the namespace in for this. And that should work. If we head over to send order email, what we'd now do is within this listener, go ahead and handle this. And that would be by sending an email.
02:27
Now, that's not what we're actually going to do here. The reason being is that we can use Laravel actions as listeners. And that's for a very good reason. We'll look at an example of that a little bit later.
02:38
So we've got this send order email listener, which we're not actually going to use. But let's just go ahead and test this whole flow out anyway. So inside of the handle of the listener that we've got here, let's just die dump on send email just so we know that this works. So when we create an order, we get die dump send email.
02:55
So the event and listener are working nicely. But we're actually going to come over to our listener section and we're going to delete this. And instead, we're going to create an action called send order email. Hopefully, you're starting to get the idea now that all of these small things can be actions and we can use them in lots of different ways.
03:13
We could even use send order email as a controller and as a listener. So let's just go ahead and create out an action called send order email and see how this kind of hooks in. So let's say make action send order email. And as we know, we're just going to pull this in from our action section.
03:32
And the namespace here is just a little bit wrong. So let's go ahead and get rid of both of these. Come down and fix this up. And there we go.
03:42
Great. So send order email is now an action. Once again, inside of handle, what do we want to do in here? Well, we want to send an email.
03:50
Let's just die dump send email action. Okay, so at the moment, if we come over and hit create order, you can see that has been handled. Now, we haven't had to add in a specific method within this action for this to be handled properly. But if you did want to specifically handle this as a listener and do something slightly differently,
04:13
what you would do is go ahead and create out an as listener method inside of here, much like we've done for as controller. Now, into this, you'll get exactly as you would normally get. And that will be an order created event, which we could use to do whatever we wanted to.
04:31
Now, in this case, what we're going to do is just defer to the handle method. And we're going to pass the event order directly into there. Now, let's just run through this one more time just in case you are not familiar with events and listeners. So over in our create order action, we are dispatching an order created event.
04:54
Over in our event service provider, this is where we would usually listen for specific events and then go ahead and dispatch things like sending an order email based on an event that we've created. So these are events and these are listeners. What we've done though is we've swapped out a traditional Laravel listener for an action,
05:14
which if we look at our send order email has a handle method anyway, which gets called. However, as soon as we implement the as listener in here, this will invoke this and we're just deferring over to handle. So if we just run this again, you can see we get exactly the same result.
05:32
But now as listener is actually being called. So let's die dump on the event here. And we should see now that die dumped out and we can do certain things in here. So there might be something you need to do in here before you then go and handle this main thing.
05:45
So let's go ahead and fill this in to actually send an email. We're going to go ahead and say mail to. We're going to from the order itself, grab the user. We don't actually have that yet.
05:56
So let's go over to our order model and let's say that this belongs to a user just so we have that information. So this belongs to and let's pass that user in there. Great. So order user and let's put in the mail facade. Now, of course, in the handle method now we're actually accepting that order in which we're grabbing from the event.
06:19
So we're not relying on the entire event being passed into handle. The event comes into as listener. But technically now this send order email action can be handled by just passing an order in. We don't necessarily replace this out directly as a listener.
06:36
So let's go ahead and send out an email here. We don't have one at the moment. So let's come over and make an email in here. Let's say order created email and let's make this markdown.
06:49
So let's choose a location for this. We'll just put this in emails and order underscore created. Okay. So let's say order created email and that should have the markdown applied in here. If we open up order created, that's just got all of this stuff in here,
07:08
which we're not going to change because we don't really care about this right now. Okay. So now that we've got this done, we can go ahead and send that email. So we create a new order created email instance. And of course, we can pass through the order.
07:21
So we get all the details in there, really whatever you want to do. Okay. So I'm not actually going to send an email out here. I'm just going to come over to the mailer and I'm going to change this over to log. That's going to add that to our Laravel log file.
07:33
So if we just get rid of everything in here, we should see that email dumped out in here. Okay. So let's go over and just try this whole thing out and see it working. So we need to come over to our dashboard, click create order. And we should see, if we open up our Laravel log, that email being sent.
07:50
And of course, it's being sent to myself with order created email as a subject. And of course, all of the body in here as well. So the benefit of this, again, is we've created a single action, which we can use on its own, but we're actually using this as a listener.
08:07
So if we wanted to, for example, just send in any order, doesn't matter how we get this, it could be from an admin panel, it could be a manual piece of code where we just pass the order in, we're able to send an order email completely detached from anything else. But this is being handled as a listener from the create order action that we had earlier.
12 episodes1 hr 2 mins

Overview

Actions are single classes that do one thing. Laravel Actions is a package that allows you to run these classes as anything you want, whether it's a controller, listener, console command – or just on its own.

Keeping your app structure to single classes like this lets you focus on what your app does rather than the design decisions around controllers, listeners and commands.

Single actions are also easier to test, and we'll also cover that!

This course is for you if:

  • You'd like to try a fresh approach to structuring your apps
  • Your app shares logic, and you'd like to combine this logic into one class that runs anywhere
  • You've heard of (or used) Laravel Actions, and you'd like a run-through
Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!