Why Use Controllers 0:00Here in my routes file, you'll notice that for each route, we handle the associated logic through a closure. And for simple projects, or even APIs and things like that, this is actually incredibly useful and succinct. So you should consider it. However, I think you'll find for most projects, this will begin to feel a little frustrating and limiting as you add more and more logic. Instead, you'll probably want to reach for a dedicated controller. Now, there are some basic ideas and practices around how you name your controllers and what they should house. Routing to PagesController 0:30Now, there are some basic ideas and practices around how you name your controllers and what they should house. And we're going to talk about that. But to start, each of the three pages we have here, they're just simple static pages, really. So often in these situations, I will use a sort of a catch-all controller, and I call it Pages Controller. Let's give that a shot. We'll say route, get the homepage, and I'm going to load a Pages Controller, and then specifically a method, or we could call it an action. And we might call that index, or even home, if you wish.specifically a method, or we could call it an action. And we might call that index, or even home, if you wish. All right, so our job is to migrate this to this. First, I want to see everything blow up. So let's watch the world burn. Give it a refresh. Ah, Pages Controller does not exist. Okay. Well, of course, we could open up our sidebar. I could go into my app directory, HTTP, Controllers, and I could manually add a new one. Generating Controllers with Artisan 1:17Well, of course, we could open up our sidebar. I could go into my app directory, HTTP, Controllers, and I could manually add a new one. But for things like this, Laravel offers generators that quickly whips up the necessary boilerplate. It's incredibly useful. Let's take a look. phpArtisan. Now, you already know about phpArtisan serve. So we're going to move on to the next one, phpArtisan make. So this make namespace, anything within here will generate a new file, whether it's a database factory or a event listener or an email.So this make namespace, anything within here will generate a new file, whether it's a database factory or a event listener or an email. Again, it feels a little overwhelming. One step at a time. You don't need to reach for any of these until you're ready. And often, you'll never use some of these commands, and that's okay, too. All right, anyways, we're going to create a new controller class using the make controller command. phpArtisan make controller, and we're going to call it Pages Controller. Okay. Adding Actions and Logic 2:05phpArtisan make controller, and we're going to call it Pages Controller. Okay. We'll find that here. So notice, here's the boilerplate you get out of the box. Now, if I switch back to my routes file, we called this method or action home. So I'm going to add a method there, home. Next, let's migrate the logic that we had here. So in this case, we just returned a view with a bit of dummy data. Okay. Paste it in. Migrating Remaining Page Routes 2:32Okay. Paste it in. And now, if I switch back, I can delete that version entirely, and this is what we get. All right. So Pages Controller, the action is called home, and we are returning a view called welcome. So back to Chrome and give it a refresh, and there we go. There's the variable being passed through. So let's migrate the rest of these over to a controller. I'm going to duplicate this two times. If you visit about, we're going to load an about action.I'm going to duplicate this two times. If you visit about, we're going to load an about action. And if you visit slash contact, we'll load a contact action. Now, for more dedicated resources, I'm going to show you a restful way to name your actions. But again, for basic static pages like this, I'm okay naming the action or the method basically the same as the route, but usually, that won't be the case, and we'll talk about that later. All right. So we need two new methods. About, that's just going to return a view called about.So we need two new methods. About, that's just going to return a view called about. And then finally, contact, and that'll return a view called contact. We're done. So if I switch back and refresh, here's the about page, the contact page. We've fully migrated over to dedicated controllers. And again, I think you're going to find for most projects you work on, not all, but for most projects of any decent size, this will be the way to go. So let's say in the future, we're going to add maybe a site for displaying projects or tasks or users. Controller Naming Conventions 3:50So let's say in the future, we're going to add maybe a site for displaying projects or tasks or users. Well, it stands to reason that you might have a projects controller, or you might have a tasks controller, or you might have a users controller. Any of these would be suitable controller names, and we'll keep talking about that. So let's move on.