Why Use Controllers 0:00This is episode 4 of Laravel from Scratch, and now we're going to learn about controllers. So I'm going to hit Command-P to go to my routes.php file. And now, right here what you see, yeah, this is inline. And this is really useful for small, simple, basic applications. Things where maybe you can build it in a few hours, and it doesn't need that much structure. But what I think you'll find is, for most projects, this probably won't do the trick. In real life, you'll probably opt for controllers. So let's figure out what that would look like. And in fact, why don't we just take this simple bit of code and translate it to a controller. Okay, so I'm going to create a new one, and I'll say, once again, Routing to Controller Methods 0:33And in fact, why don't we just take this simple bit of code and translate it to a controller. Okay, so I'm going to create a new one, and I'll say, once again, when we make a GET request to the home page, and of course, don't forget, there is a POST request, there is a PUT request, a PATCH, a DELETE. If you're familiar with the concept of REST, you'll know how to get up and running. Otherwise, I'm going to teach you all of that. So let's bring it back to GET, and now, rather than referencing a function, I will reference a controller name. How about something like a pages controller? Now, we haven't created this just yet, but we will in a minute.How about something like a pages controller? Now, we haven't created this just yet, but we will in a minute. Now, we can specify the method Laravel should call by using the at symbol here. So in this case, this is the home page, so why don't we just call the method home. Alright, so I will comment this out, switch back to Chrome, and we should see an error, right? If we try this out, yes, we do. And this is what an error looks like when you come across it in Laravel. So you get a little whoops screen, and it will tell you the line and the file where the error occurred. In this case, yeah, a pages controller does not exist, so we need to generate one. Now, where do controllers go? Generating a Controller 1:38In this case, yeah, a pages controller does not exist, so we need to generate one. Now, where do controllers go? Well, they're going to be in your app, HTTP directory, and controllers right here. Now, notice you'll see one out of the box. This is sort of like your master controller. All other controllers will extend it, and this way we can pull in some common functionality that most controllers will make use of. Now, it's true we could manually create a new file, but because this is so common, Laravel can help us out. Let's run phpArtisan, the command line tool, and if I scroll up, yeah, notice all of these generators here. Anything prefixed with make will create a file with a complementary stub. So in this case, take a look at this.Anything prefixed with make will create a file with a complementary stub. So in this case, take a look at this. Create a new controller class. Now, if you ever need more information, for example, does this accept any arguments or options, what you can do is say phpArtisan help and then the name of the command, like this, make controller. All right, so all we have to give it is an argument with the name of the class. Laravel will grab that value, inject it into a little stub, and then create the file for you. All right, phpArtisan make controller, and we're done. So now if I come back, you'll see we have this new file right here with a bit of boilerplate to get you up and running. So notice that we've namespaced this. Implementing the Home Action 2:56So now if I come back, you'll see we have this new file right here with a bit of boilerplate to get you up and running. So notice that we've namespaced this. Now, I'll probably do a couple minutes on namespacing just to make sure we're all up to speed, but we'll do that in a future video. Next, remember how I told you all controllers will extend this parent controller? We're doing that. And now because these two are in the same directory, I could even remove that import. And in fact, in this little example, we're not making use of the request object just yet, which I will teach you about soon, so I can remove all of that. All right, so we said we wanted a method called home, so we'll create a method called home. Next, at this point, this is basically equivalent to what we would have had right here, Adding an About Route 3:29All right, so we said we wanted a method called home, so we'll create a method called home. Next, at this point, this is basically equivalent to what we would have had right here, which means we can grab all of this, delete this section entirely, and now if I come back, I can paste it in. All right, so that means if I come back to my browser and give it a refresh, yeah, we get the same thing as from the previous video, but now you're successfully using a controller. So let's see. If we want an about page, it's going to be the same flow. When we go to about, that's equivalent to a page. So we can put it in the pages controller because it's mostly static information. And then you go back to your controller.So we can put it in the pages controller because it's mostly static information. And then you go back to your controller. You create the method about. And real quick, notice how I used a snippet there? Always do stuff like that. If you're manually writing this out, yeah, it's just kind of slow and it's bad for your fingers. Because you'll create methods nonstop, yeah, I have a little snippet here. You can create it in any editor at this point. In my case, it's M-E-T, tab, and it will build that out. About.In my case, it's M-E-T, tab, and it will build that out. About. And now once again, we could return a string, about page, or you can load a view. So let's come back, visit about, and there you go. It's working. All right, so I hope you're working along. Try this out. Try to get comfortable with this entire workflow. And when you're ready, let's learn something new.And when you're ready, let's learn something new.