Adding Create Action0:06
All right, so if you take a look, we should ultimately have seven resourceful actions. We have one for index, two for show, three for edit, four for update, five for store, and sixth for destroy. Where's the seventh one? Well, the seventh one would be where you display a form to create, um, a new instance. So we have one for editing, but we don't have one for creating. And that's because we put our create form, uh,
but we don't have one for creating. And that's because we put our create form, uh, directly within this view. And that's totally fine. But you could also do something like this. We can make a new one, call it create and say, well, if the user accesses ideas slash create, this should display a form to create a new idea. And when they submit the form, that would then hit the store action.
Creating Create View0:52
And when they submit the form, that would then hit the store action. So in fact, let me grab that and move it up right here to make it easier for you. When you submit this form, it'll make a post request to store and persist the idea. Alright, let's do that now. Ideas slash create. Let's come to our view and we will duplicate index again, create that blade and yeah, let's just clean it up.
and we will duplicate index again, create that blade and yeah, let's just clean it up. Let's see, we're gonna have, create a new idea or create new idea. Um, this looks actually pretty good to me. That's gonna make a post request. Yeah, this is the exact form that we created for the index page. So if you want, uh, we could delete this entirely from the index page.
Handling Empty Ideas1:39
So if you want, uh, we could delete this entirely from the index page. Let's have a look, come back, give it a refresh. And now you know we don't see anything, right? Because we don't currently have an idea. So we should handle that here. We're checking if ideas count, why don't we add another one and we can say, well, if we don't have anything at all, then let's say no ideas yet. And then we'll have an anchor tag that goes
let's say no ideas yet. And then we'll have an anchor tag that goes to ideas slash create. Create a new one, alright, to the browser and yep. Uh, let's silo this just a little bit and we'll be done. Why don't we make it underlined by default? And then on our layout file, let's set sitewide that the tech should be white by default. All right, we don't have any ideas yet.
that the tech should be white by default. All right, we don't have any ideas yet. Let's create a new one. We click on that. That takes us to ideas slash creates, which has a form, build a boat, click save that persists. This looks good. Um, we can click on any of these. We can edit, we can update. This is all looking good to me. So now if I switch back, we have seven resourceful actions. But as you can imagine in any application, you're gonna have
Why Use Controllers2:50
resourceful actions. But as you can imagine in any application, you're gonna have more than one resource. You're gonna have lots of resources, dozens, maybe hundreds. So there's just a lot going on here, right? And we only have a single resource to start. So I think you'll find for most non-trivial projects, for, for most non tiny things or demos, uh, you'll find that developers prefer controllers.
Generating Resource Controller3:13
or demos, uh, you'll find that developers prefer controllers. A controller is just a class that is a container for any action that can respond to a route. So I'll show you if I run PHP Artisan and scroll up, you'll see once again we have all of these different make commands. Anything proceeded with make means we're gonna scaffold a file, uh, make an event class. When you learn about that, make a generic class, make um,
scaffold a file, uh, make an event class. When you learn about that, make a generic class, make um, a listener, make a middleware, make a migration. In our case though, I want to make a controller, so I'm gonna run PHP Artisan, make controller. Think of these as glorified snippets that will auto-populate the file. You don't have to reach for these, but most people do. What is the name of our controller? Well, it's a controller for working with ideas.
What is the name of our controller? Well, it's a controller for working with ideas. So we're gonna call it idea controller. Alright, so that goes with an app, HTTP controllers. Let's take a look. App HTTP. And now we have a new idea controller. Now there's actually a couple things we can do here, a few configuration options that you should be aware of. Let's delete that entirely. And if I were to delete this entirely,
Let's delete that entirely. And if I were to delete this entirely, then Laravel is gonna ask me, well, what should we name it? Idea controller. And then it will ask me one more thing. Do we want just an empty controller? Do we want this to be a resource? Should it be invo? A singleton? Some of these are a little higher level for now, empty is just what we just saw, just a plain old empty controller that you can populate by hands.
just a plain old empty controller that you can populate by hands. But we've been working with resources, so let's create a resourceful controller. So what model should this resource controller before? What is the corresponding model idea? Alright, so now take a look. If I switch back, you'll see that it has populated this controller with the seven restful actions, right?
Migrating Routes to Controller4:59
that it has populated this controller with the seven restful actions, right? And notice again, it knows to leverage route model binding because everybody does. It's so much better. So cool, right? So now think about it. I'm gonna open this in a split. I'm gonna return to my routes file and we can just migrate all of these over one by one. Alright, so let's get started here. Index action. Well, we named it here.
Alright, so let's get started here. Index action. Well, we named it here. Very helpful that we thought to do that. So I'm gonna take this and I'm just gonna move it over to our controller action. And now instead of using a closure to handle our route, I'm gonna use our controller. And we do that like this within an array. It's kind of like a tuple. I'm gonna reference the controller we care about and too much ai.
It's kind of like a tuple. I'm gonna reference the controller we care about and too much ai. Uh, we're gonna use the controller string name that we care about, and that's why we use class. I want the string path. And then I want the name of the action that we wanted to call. If we were to come back, give it a refresh, it's still going to work, but now we're using our dedicated controller. Hello, Sandy, to check. Are we hitting the controller?
to work, but now we're using our dedicated controller. Hello, Sandy, to check. Are we hitting the controller? Yes, we are. It's working. Cool. Let's keep going. What about this one? Show a form to create, uh, a new idea done and replace this like, so idea controller create. All right, let's do store. And there we go. Store or persist a new idea. Paste it in, like so update this. You get the idea. So if you understand what I'm doing here, feel free to fast forward about 30 seconds.
You get the idea. So if you understand what I'm doing here, feel free to fast forward about 30 seconds. Otherwise, yeah, it's pretty much rinse and repeat show. I'm gonna take all of this, come down and we're gonna show a single idea and then I'm gonna update this. All right, AI's pretty smart here, so it's making it a little quicker than usual. All right, let's edit an existing one. Good, there we go.
All right, let's edit an existing one. Good, there we go. And now update. Almost done folks. Paste that in. Replace this like so. And then finally we have destroy, destroy it. Come down. That's done. Update this. And now notice that our, uh, comments here are a little bit redundant, right? So I can get rid of that, that, that, that, that, that and that.
So I can get rid of that, that, that, that, that, that and that. And you know what? If you want, you can even make this a little bit simpler by reaching for a route resource and you're free to research that on your own. But you know what? From experience, I kind of just like being a little bit verbose with my routes. Uh, a couple years from now, you'll appreciate that you have a single file that lists every single route. And look how clear this is.
that you have a single file that lists every single route. And look how clear this is. So because we're following this resourceful pattern and we're we're using rest, there are conventions that we can reach for. We don't have to reinvent the wheel. My resource name is ideas, which means I will always use this endpoint to fetch all or collection of that resource. If I need to create one, I will always use this convention.
or collection of that resource. If I need to create one, I will always use this convention. If I need to update one, I will always use this convention. And the same is true for my controller actions as much as I possibly can. I will always stick to these seven resourceful actions. And I would never create, oh, never say never. But 98% of the time I would never create a different action name. Um, if I wanted to create a different one,
of the time I would never create a different action name. Um, if I wanted to create a different one, I would create a brand new controller and then choose one of these seven resourceful actions. Alright, we have now switched over to controllers, but there's more to do. I'll see you in the next episode.
