This episode is for members only

Sign up to access "Laravel Actions" right now.

Get started
Already a member? Sign in to continue
Playing
04. Using actions as a controller

Transcript

00:00
We're now going to dramatically change what we've done here. And we're going to get rid of this controller completely
00:05
and instead reference the createPost action as if we were referencing a controller. So let's head over to our web route and just start the process of doing this.
00:15
It's not going to work at the moment. But then as we start to add specific methods in to handle this like a controller, we're going to end up getting this to work.
00:24
So we're going to reference the createPost action directly inside of here. Now at the moment, if you've not worked with actions and specifically not worked with this package before,
00:33
this probably doesn't make any sense because the createPost class is just that. It's a class with a trait inside of here and a handle method.
00:42
We're not using a invoke magic method here or anything like that. So what we're going to do is just try and run this, just see what happens.
00:49
So we're going to come over to the browser. We're going to enter anything in here and hit create. Okay, so we've got too few arguments to function createPost handle.
00:57
That kind of makes sense because handle isn't something that Laravel will handle by default when we reference a class inside of here. It would need an invoke magic method
01:06
or you'd need to reference the method within this controller specifically. Okay, so how do we make this action handle as a controller? Well, let's go ahead and add in a method in here
01:19
called asController. And let's just die dump in here and say controller and just see what happens. So if we come over here and go to the dashboard again
01:30
and we hit create, there we go. So what's actually happened is the Laravel actions package is now invoking this asController method when we use this action as a controller.
01:42
So what does that mean? Does it mean that we then grab this code here and do this and then go ahead and return back? Not quite because the way that this is going to work
01:53
is we're always going to have this handle method in here. This is like the root of what we want to do within an action. AsController is going to call this handle method
02:05
passing through all the data it needs. Now you might also be wondering, well, what about validation? Can we say this validate inside of here?
02:14
Well, not quite, we'll get to that in just a second. So let's make this work first of all without any validation and then we'll come back to this. So as the Laravel actions package invokes this,
02:26
we're going to get through a request object much like we would in a controller. Of course, we just need to make sure we pull in the namespace for this at the top here.
02:35
Okay, so as controller, what do we want to do? Well, we want to call this handle and we want to pass through the data that we need. So in this case, it's going to be
02:44
the currently authenticated user, which we can get from the request and it's going to be the data that we want to send through to this.
02:50
And in this case, we only want the body and here, of course, it depends on the kind of data that you need to fulfill this handle method. Then we can do things like return back.
03:02
That's going to work in exactly the same way and we can even go ahead and type in this if we wanted to, to a redirect response under illuminate and HTTP.
03:12
You don't need to do that, but it's nice to have. Okay, so now that we are invoking as controller when we use this like a controller and we are using the handle method directly from here,
03:24
then this is going to work in exactly the same way. Let's go over to the dashboard once again and let's try this out. So another post, hit create, and there we go.
03:34
You can see that has worked nicely. So although this might feel a little bit weird at first, if you think about it, it makes a lot of sense. We've created an action which does one thing,
03:45
but we can also use that action as a controller. Of course, we can use it on its own if we wanted to somewhere else. In fact, the controller that we've already created
03:54
that does this will still work. So we could swap this out technically to do this as well. Okay, there's one very crucial thing missing here that we were able to very easily implement
04:05
inside of our post store controller and that is validating data. So let's see how we can validate data directly within our action in the next episode.
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!