This episode is for members only

Sign up to access "Laravel Actions" right now.

Get started
Already a member? Sign in to continue
Playing
09. Actions as commands

Transcript

00:00
Let's say that when the user created an order, this email failed to send or we just wanted to resend this email to a user for whatever reason. Well, what we might do is either reach for an admin panel or reach for an artisan command to send this.
00:16
So we might have some kind of send order email and we might pass into this the id of the order. So that would resend that email for us. Now with Laravel, we can create commands specifically to deal with things.
00:30
You may or may not have done this. That's all in the documentation. Or what we could do is allow this send order email action to be called as a command. So as well as having something to just send an email by passing the order in,
00:48
using this as a listener, we can also use this as a command. So let's see how we can do that now. So the first thing that I'm going to do is go ahead and introduce the method to make this work. As you'd have guessed, based on the sort of standard we've already seen,
01:03
we're going to use as command. Let's dive up here and just say command to send order email, just so we know what we're doing. Now, how do we register this whole thing as a command, potentially as a command?
01:16
Well, if we come over to the kernel under our console, what we can actually do is register this command in here as a property or under a property. So it doesn't exist in here at the moment. But what we can do is just up here, create out a commands property.
01:34
And we can just pass the fully qualified namespace to that command. And that will be registered for us. So we're going to say send order email, pass that in. And let's now go and run phpArtisan.
01:45
OK, so this isn't working at the moment, because we have a command signature missing from this action. So let's go back over to the action, fix that out first. And then we'll get this working.
01:57
So to create the signature for this, which is really important, because it's how we call it and it takes in the parameters that we want, we're going to go ahead and create out a command signature property inside of here. And this works exactly like a normal Laravel command if you've created one before.
02:14
So let's think of a name that we want for our artisan command. Let's say orders and send order email. And then, of course, we want the order ID to be passed in that we can pick up and actually send for the correct order.
02:29
So I'm going to go ahead and call this order underscore ID, although you can call that whatever you want. OK, let's go ahead and run this command again. So let's just run phpArtisan on its own.
02:38
And sure enough, if we head up and look for orders here, we should see our new registered command. Great. So we can now go ahead and say phpArtisan orders, send order email,
02:52
and we can choose the ID of the order that we want to send this for. So let's go over to our orders. We've got a load in here, but let's just choose order one just to make this simple. And let's just hit this.
03:03
And there we go. We get dyadumped command to send order email, which, of course, we are doing within this as command section just here, this method just here.
03:13
Just a side note, if for any reason you needed to dynamically generate this and you didn't want to add this as a property, what you can actually do is within this, generate out a or introduce a get command signature method.
03:28
And that's going to do exactly the same thing, but then gives you the opportunity to generate this however you want. It's really up to you whichever method you use. In this case, there's an option.
03:39
So let's go ahead and leave it like that with get command signature. I think that looks a little bit nicer because it kind of follows the same convention as just creating all of these methods out.
03:48
So I'm going to put that at the bottom here. You can see if we just run this again, it works in exactly the same way. Okay, so now what we want to do is go ahead and take the command just here, which is the console command class from Laravel.
04:05
So that's just here. If we pull the namespace in, it's this one. What we can now do is just use this like a normal command if we had generated a standard command class.
04:16
So again, what we want to do is defer this to calling handle because remember handle is the kind of root of everything that we do. So under as command, we're going to go ahead and say this handle. Well, what do we need to pass through to handle?
04:29
Well, we need to pass through an order. So how are we going to get this order in here? Well, in this case, we're just passing through an order ID. So just before we call handle, let's die dump on command
04:41
and we're going to say argument and order underscore ID. Let's just see what we get when we run this. We get one. So effectively, what we need to do is when we handle this,
04:51
look this up in the database so we can pass the object for the order through to handle. So we would just say order, find or fail just to be safe. And we would use that command object to grab the argument for the order ID. And that is pretty much it.
05:08
So let's go ahead and clear out our Laravel log just so we know this gets sent. And now, as well as using this as a listener, we can go ahead and send an email manually using a command. Using, remember, the exact same code for both instances.
05:25
Now, of course, because we have access to this command object, what we can do is do everything that we would normally do within a Laravel command. And that includes things like outputting to the console. So, for example, I could use command info and I could say order email sent.
05:43
And that would do exactly as we would expect from a Laravel command. Let's do this again and run this. And there we go. Sure enough, we have our output.
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!