Lessons: 21Length: 2.4 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.4 Routing to Controllers

We've used closures to handle our requests thus far, but when you want to handle several different requests for the same resource, a controller makes more sense. You'll learn how to create a controller and set up routing in this lesson.

2.4 Routing to Controllers

So far, we have been using closures to define our routes, and for small applications, that's perfectly fine. However, as our applications grow, as we add more routes, and especially as our routes become more complex, this can be very cluttered. So what we want to do is start to organize our code into logical chunks. For example, there are three routes, two of them are somewhat related. We have the welcome page, and then we have the about page. And even though those are completely different routes, they serve somewhat the same purpose, in that they provide content for, well, kind of the home parts of the application. It's not necessarily a homepage, but there's the welcome page, there's an about page. If we had a contact page, I would say that this is part of the home content. So let's add a contact page, that would be nice to have. So if there's anything that was just generic to the website itself, then I would consider that part of the home content. And so we could put those routes inside of their own file. Then we would have a separate file for the store routes. And any other routes that we would need to write for the store, we would put inside of that same file. If we had some other content that wasn't related to the home stuff or the store, but let's say that we had some information about different kinds of collections. There are some music stores that will have a collection of stuff that they don't sell, but they still collect it. So if the store collects guitars, then we could have a guitars routes. And then from there, there could be individual pages for individual guitars that they would want to provide content for. Now, so that would be in a completely separate file because that had nothing to do with the home stuff or the store, it would be just the guitars. So we want to start to create these new files that contain the functionality for these routes. Because in actuality, we still need to define our routes here inside of this web.php file, but as far as the code that's handling those routes, we want to define that elsewhere. So we are going to create what are called controllers. Now, if you'll remember from a few lessons ago, I said that Laravel is an MVC framework, that stands for model-view-controllers. Controllers are classes that have methods, we call them action methods. And those action methods handle requests, and that's basically it. It sounds very simplistic, and in a lot of cases, it is that simplistic. However, in other cases, it's not. So in this lesson, we are going to create a home controller for all of this home oriented content. So let's go to the app folder, let's open up Http, and then there's going to be a controllers folder. And you can see that there's already a file in here called controller.php. This is the base controller class that you don't have to use. In fact, you can write a controller that would be just a normal class, it doesn't inherit from any other class, and you would be fine there. But this controller class has a lot of utilities that you would probably want to use. So it makes sense to create a controller class that would inherit from this. So we could create a new file here, we could call it home controller. The name really doesn't matter, but remember that we want this to be logical, we don't want to just call it something random. This is stuff oriented to the home content. So we started with home, and we followed it up with controller. So we know that this is the controller for all of the home stuff, it just makes sense. So we could take this route or we could use artisan, artisan is the CLI for Laravel. And we're going to use artisan because it's a fantastic tool that gives us a lot of functionality. In fact, if you just type php artisan, you're going to see everything that it can do for us. There's quite a lot, even just the make portion, which is what we want to do because we want to make a controller. But you can see that there's a lot of things that you can make with artisan. And the great thing is, every one of these commands have their own options. And artisan is just going to stub out exactly what we want it to. So let's look at php artisan make:controller -h, that's going to show the help for the make controller command. And you can see that there are several other things. One of note is this resource. We're not going to look at a resource controller class in this lesson, but we will definitely use this, and you will definitely use it in your applications. And the great thing about this is if you do use the -r flag. It's going to generate a class that has all of the methods for creating a resource controller. So artisan is a fantastic tool, we need to use it. So php artisan make:controller, and then we just follow that up with the name of the controller that we want to create, HomeController. And then there we go, we have our class. If we go back to our code, we can see that there is now a HomeController.php file inside of our controllers folder. It gives us the class it inherits from controller. So now, we just need to write our action methods. So we will start with the welcome page, which we could call welcome. But since this is kind of the homepage or the index page, I guess we should use index page. I'm going to call this index, that's just convention. You don't have to follow conventional, but convention is there. Well, because it's convention. And all we are going to do is return the view. We are basically going to take the contents of this closure. We could just cut that out, we could paste this here, and then just give it the name of index. So we will say public function index, and then voila. We'll do the same thing for the about and contact. So let's cut, and then we will paste, and let's give this a name of about. And then finally, we will do the same thing for contact. But since we already have that functionality, we'll just copy about. So there we go. We have our controller, all we need to do then is link the action methods that we've defined here to the routes, and we do that very easily. The first thing we need to do is import that HomeController. So that is inside of App\http\Controllers and then we will bring in HomeController, and this is what we will do. We are still going to say, route and then get, we still specify our URL. But the second argument that we pass is going to be an array, where the first element is the class that we are going to use, HomeController in this case. And then the second element is going to be the name of the method that we want to use, so index in that case. So we'll just copy that, paste it a couple of times, and then make the necessary changes. So for about, it's going to use the about method, and then contact will use the contact method. So this is much cleaner, and our code is still going to work just like it did before. So if we go to the browser, let's refresh this page. And well, we have something wrong, App Http Controllers. It's amazing what one keystroke will do, so there we go. We have that. If we go to about, we're going to see our about page. If we go to contact, which we just added, we will see our contact page.

Back to the top