Why Use Controllers 0:00So far, we've been storing our logic, our controller logic, really, as it turns out, directly within the routes file. And for, I would say, smaller projects, this is really nice and succinct. So maybe if you're building a small little web service, a little API, a little static website, this is really great. Remember, some of this can get complex, but the goal is to always keep it as simple as you can possibly justify. However, that being said, for most projects, this very quickly will get pretty unwieldy as you add more and more for different sections of your website. Yeah, you probably don't want that. So instead, we can introduce controllers. And controllers, I sort of think of controllers as the middleman. So a controller will receive a request, like a request for the homepage, and it will figure out, okay, well, I need to compile this data from here. So I'm going to ask my, maybe my task model or my post model to give me the Controller-Model-View Flow 0:47a request, like a request for the homepage, and it will figure out, okay, well, I need to compile this data from here. So I'm going to ask my, maybe my task model or my post model to give me the data I need. And then once I have that data, I'm going to pass it to the view or the presentation layer, and then the view can render it onto the screen. So you notice we have these different layers. The controller layer delegates. The model layer can be responsible for the domain and fetching any data we require. And then the presentation layer, traditionally, you can associate with the HTML. Okay. So if we want to swap this out, we could say, when you get a request to tasks, well, what kind of controller do we want to use? Well, once again, what is it a controller for? It's a controller for tasks. So why don't we call it tasks controller. But not just that, we need to tell it, well, which method on the controller corresponds to this. And generally, Generating a Controller 1:32for? It's a controller for tasks. So why don't we call it tasks controller. But not just that, we need to tell it, well, which method on the controller corresponds to this. And generally, the method, and this is basic REST that we'll talk about quite a bit more in upcoming episodes. But the method that is responsible for showing all of a resource, you will often call index. Okay. But where do we keep the controllers? Well, they will be in our app, HTTP, because this is the HTTP entry point into the app. And then you'll see a controller's directory here. Okay. But how do we create one? Do we have to manually do it? No, you can use a generator. So once again, let's look for make, and you'll see that we have a make controller generator. phpArtisan make controller, and it's called tasks controller. Okay. Now you'll see that pops into the controllers directory. All right, we need to add a index method. And now you can think Moving Routes to Controller 2:19phpArtisan make controller, and it's called tasks controller. Okay. Now you'll see that pops into the controllers directory. All right, we need to add a index method. And now you can think of everything here as being equivalent to what you stored here. So let's cut that out and paste it in. However, notice task, we got to make sure that we import that. So let's say use task. All right. So we come back, that can be removed. And now let's do another one for this method here. If you visit tasks slash some particular task, well, what are we doing here? We are showing a task. So the common convention is to name that method show. Okay. Let's create another one. Show. And once again, we can take all of this and move it over. Let's come back, delete this entirely. And yeah, doesn't that feel quite a bit more clean? We can even get rid of this at the top. Nice and simple. So we have URI and a controller method Testing Controller Routes 3:14delete this entirely. And yeah, doesn't that feel quite a bit more clean? We can even get rid of this at the top. Nice and simple. So we have URI and a controller method that is responsible for it. And that's this one. Next, we have another endpoint and another controller method that's responsible for handling that. And here we go. And once again, notice how the controller delegates. The controller receives the request that we'll talk about later. And it knows, okay, well, for this endpoint, I got to fetch a task. So I'm going to ask the task model to give me the task in question. And then I'm going to take that and I'm going to pass it off to the presentation layer so that the view can render the necessary HTML for the user. It delegates. Let's save it and make sure everything worked. We give it a refresh. And there we go. Let's click on one. Undefined variable ID. This is because we're expecting an Fixing Missing ID Parameter 3:55It delegates. Let's save it and make sure everything worked. We give it a refresh. And there we go. Let's click on one. Undefined variable ID. This is because we're expecting an ID, but we forgot to accept it. Okay. Give it a refresh. And there we go. So we have exactly what we had before, but now we switched over to using controllers, which is what I would very much recommend. Now, in the next episode, I'm going to show you route model binding, which sounds scary and confusing, but you know what? It's just jargon. It's just a term that you're going to love. So stay tuned.