This episode is for members only

Sign up to access "Laravel Actions" right now.

Get started
Already a member? Sign in to continue
Playing
03. Resolving actions from the container

Transcript

00:00
One of the benefits of actions that we've already spoken about is that they're easier to test. So even if you did want to use them in the controller like this, or in a controller like this,
00:10
that's absolutely fine. But there's one way that we can grab these out of the container, which makes a little bit more sense, and it tends to read a little bit better as well. So let's go ahead and do this just to tidy this up,
00:22
and we're going to go ahead and take createPost, we're going to inject it into here instead, give it a name, and then use that to run the action. So a very, very simple change, but something that makes a little bit more sense in terms of passing particularly dependencies
00:38
down to other classes where you are going to be using actions within other classes. So what we're going to do here is we're just going to go ahead and reference createPost, and we're going to call this createPost. Now the naming convention I tend to use here is give this the exact name
00:54
of the action you have created, because then createPost is very, very clear here. So we're just going to swap this out here with createPost, and we're just going to invoke run inside of here. Now doing this in a controller is fine,
01:08
because we would probably test the controller at the controller level to check validation, check that this was created. But if you were passing through the createPost dependency to another class, or if you were retrieving this from the container from within another class,
01:25
then it makes a lot more sense to pass these into a kind of constructor or invoke. So let's head over really quickly and just say post to create this out, and of course it works in exactly the same way, and we have that post created inside of here.
01:40
Of course what you could also do if you needed is apply this to the constructor. So you could take this if you had other methods within a controller, for example, that made use of creating a post. You could just go ahead and either say public createPost or protected getPost,
01:57
depending on the PHP version you're using, or inject that in and then go ahead and assign it with this and createPost like so. Again, depending on the PHP version you're using. I'm just going to go ahead and use construct a property promotion
02:12
to apply the visibility I want to see. I don't need to define this as a property now, and that's now going to be available. So what we could now do is say this createPost,
02:20
and that's going to work in exactly the same way. So if we come over here and just say post three, sure enough, as you probably already guessed, this works in exactly the same way.
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!