This episode is for members only

Sign up to access "Laravel Actions" right now.

Get started
Already a member? Sign in to continue
Playing
10. Actions inside actions

Transcript

00:00
So far, all of the actions that we've created are only doing one thing. So we've got create order, create post, and send order email.
00:08
That's pretty much how actions should be, but it doesn't stop you from using other actions inside of actions. So in this episode, we're going to take a look at an example of this. And when we get down to looking at testing actions,
00:20
we'll build a kind of real world example of this. So I'm going to go ahead and start out by creating out a new action. So let's make an action in here. And we're just going to call this do something,
00:30
just to look at a really simple example. So if we come over to the do something action, of course, we can define anything we want inside of handle. We can define this out as a controller, a command,
00:42
anything that we've already looked at in this course. But what happens if we want to do something in here, perhaps with some data that came from somewhere else, but we wanted to be able to fetch data as an action?
00:54
Well, let's create out another action in here. And we're going to say fetch something from somewhere. Maybe this was from an API or something like that. So what we want to do is use that inside of here
01:07
and then do something with the data that we get. So to demonstrate this out, let's go over to our web routes. And let's just create out a route in here. And we'll just call this inside,
01:19
just to very quickly demonstrate this. And we're going to go ahead and use that do something action. And we're just going to run this. That's pretty much it.
01:29
So we can go ahead and run this on the command line, or you could run this in your web routes. It's entirely up to you. And if we just go ahead and run inside,
01:39
of course, at the moment, we get nothing. So the goal of this, if we just var dump this out, is going to be get something from an action somewhere else and then just return it from here.
01:49
So let's go over to our fetch something from somewhere. This is pretty straightforward. The implementation here doesn't matter. But we'd probably want to return an array here
01:58
or some sort of data wrapper, some sort of object with all the data that we needed. Let's just mock this out by saying id of one for now, just so we've got something to play with.
02:08
So inside of our do something action, we would want to use this. Maybe this fetches plans from a payment provider like Stripe, which is the example we're going to look at really soon.
02:20
Or it hits an API to gather some data, maybe reads a file, something like that. Well, inside of here, we can do this two ways. We could go ahead and say fetch something from somewhere
02:32
and run, so just call it as we normally would, and assign the data we get back from there. And then go ahead and do something with that data and maybe return it back or return the newly created data.
02:44
So we do something here, which is an action in itself. We do something with the data inside of here. And then we return something else, in this case, just returning the data that we've got.
02:55
So if we now head over to the command line here and run this, sure enough, we get back everything that we defined or returned from this action just here. So although actions should do one thing,
03:06
which technically our actions are still doing, you can use actions within actions, no problem. Now, there's another way that you can define this out rather than having to call run just here.
03:17
And when it comes around to it, it really depends on how complex things get and also the testability of these. So we can go ahead and define out a constructor in here.
03:27
And we know that we can grab these from the container. So I can go ahead and say, fetch something from somewhere. Let's go ahead and make this protected. And we can come down here.
03:37
And instead of this line here, we just copy this and comment it out. Instead, we can say this, fetch something from somewhere, and we can go ahead and call run.
03:47
So if we just go ahead and run that curl request again, you can see it works in exactly the same way. So we can use dependency injection in here if we want to, or we can just call run like this.
03:58
When we get around to testing these, we'll see how to handle both of these scenarios, depending on which one you choose. So there we go.
04:06
You can use actions within actions. Just make sure that you're not doing too many things inside of one action as you start to build this out. Although something like this and fetching from a data source
04:17
and then doing something with that data is perfectly acceptable. If you find yourself doing too much inside of one action, consider breaking that up into multiple actions that you can run within your controller.
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!