Avoid Hardcoded URIs 0:00Here's another technique. Now if I scroll down to the update method, after we update the article, we redirect to this page. And further, if we go to articles slash index, yet again, we write out that full path. So now think about it, what if at some point in the future, we change this URI? Maybe it becomes slash blog slash articles slash article. Alright, well now, every location where you hardcoded that URI will need to be updated. Now in the grand scheme of things, if you're not running a massive site, it's not a huge deal. But nonetheless, it can be a pain. Using Named Routes 0:35deal. But nonetheless, it can be a pain. So if you want to get around that, you can instead use something called a named route. And here's how. After we declare the route, I'm going to give it a name called articles dot show. Now you can use any naming convention you want, but just be consistent about it. In this case, I'll do a similar approach to how we name our views. So now I can reference this URI by using the name here. For example, if we go to articles slash index, right here, I'm going to swap it out with the named route, like this. Passing Route Parameters 1:01For example, if we go to articles slash index, right here, I'm going to swap it out with the named route, like this. Route articles dot show. However, if you think about it, this route requires a wildcard, right? So if we switch back, we still have to tell Aerofl what that wildcard is. So you can do that as the second argument. You can do it in two ways. First, you can provide it explicitly, and that'll work. However, we could also just provide the article itself, and Aerofl will automatically know to fetch the correct key name.However, we could also just provide the article itself, and Aerofl will automatically know to fetch the correct key name. So now, if I come back to the articles page, any of these will still work, just like they did before, but now I'm not hardcoding that URI. All right, so now, if I go back to the controller, we can update this redirect, like so. Route articles dot show, and then pass through the article. So you might want to update this one as well, or if you want to stick to hardcoded URIs, if you don't see yourself changing these paths very often, that's okay. I wouldn't be religious about this either way. Mostly it comes down to your needs and your preference.I wouldn't be religious about this either way. Mostly it comes down to your needs and your preference. But yeah, if we took that approach, you'd give all of these their own named routes, like this. Articles dot index. And if I scroll back, this would then become a route. Now I'll finish up by showing you a slight variation. So one thing you'll find in your projects is frequently you are linking to a specific resource. So give me the link to that article, or that project, or that user. Adding Model Path Method 2:30resource. So give me the link to that article, or that project, or that user. So often, what I will personally do in my own projects is visit my model and add a method called path. Here I will return a string that represents the path to that specific article. Now here, if you're not using named routes, you could hardcode it, or you can do both. You could return a reference, like so. And then pass through the article, which would be the current instance. And you get something like that. So have a look.And you get something like that. So have a look. If I boot up phpArtisan Tinker, and we track down an article, we'll grab the first one. All right. Let's get the path to the article. And that's what we get. Now I often will reach for this because I think it does improve clarity a little bit. So for example, things like this, route articles dot show past the article, it's fine. It's just a little more verbose than saying redirect to the article's path. Personally, I like the way that reads. Controller Cleanup Recap 3:26It's just a little more verbose than saying redirect to the article's path. Personally, I like the way that reads. And the same would be true for articles slash index. Keep it like this if you want, or change it to the article's path. All right. And that'll do it for your first set of techniques for cleaning up your controller. So here's what we ended up with. Have a look at this. There are a couple other things we might consider, but at this stage, I actually think this looks pretty good.There are a couple other things we might consider, but at this stage, I actually think this looks pretty good. So in the next episode, let's move on to something else.