Why Use Named Routes 0:00Alright, let's talk just a little bit about named routes. So I've commented out the resource section temporarily. Let's write this out manually. If I visit songs, then I want to visit songs-controller-at-index. So if we switch over to the browser and refresh, that still works. But now, if we click through, of course we don't have a URL for that. So let's add that one. And then an identifier. And that will trigger the show method. So if we come back, now that one is working.And that will trigger the show method. So if we come back, now that one is working. And all of this seems to be working great. However, the problem is, if I go to the index view, we are hard-coding that URL in. So as an example, imagine that we decide to switch this out for music. Music and then an identifier. And we'll just call that song. Well, of course, if we switch back, this is no longer going to work because we changed the resource name. But also, if we visit music and we click through, that's not going to work as well because,the resource name. But also, if we visit music and we click through, that's not going to work as well because, once again, we hard-coded this URL in. So that means if you ever have to make a change like this, you would then have to update everywhere in your application where you referenced or hard-coded this URL. Not always the best idea. So instead, here's what we can do. We can give these names. For example, I could say I want to refer to this route as songs-path. And then that will use this controller that we see here.For example, I could say I want to refer to this route as songs-path. And then that will use this controller that we see here. All right, let's do the same here. This will be song-path. And this is just a convention I use. You can use anything. I'll even show you a different one shortly. But anyhow, that will use songs-controller at show. So now, to show you how this works, if we were to run phpArtisan route-list, notice that yes, we have these two routes, however, we've now assigned them names. Generating URLs by Name 1:48So now, to show you how this works, if we were to run phpArtisan route-list, notice that yes, we have these two routes, however, we've now assigned them names. That means if we go back to our index view, we no longer need to hard-code this in. Instead, I can simply replace it with the name that we assigned to the route. And to do that, we can use this helpful route function. And that'll return a URL to a named route. So in our case, we want to go to the song-path, but also don't forget that to link to a song, we also have to send through an identifier. So we need to pass that through to song-slug. And that's it.So we need to pass that through to song-slug. And that's it. So now, this is a lot more flexible. If we were to come back and try to click through, it works. However, if we go back to our routes file and decide to bring that back to songs, well, now we visit songs, and the URLs will automatically be updated. Click through, and it works. Now let me show you some ways that we could even clean up this HTML a bit. Do you remember when we worked on the edit page, we pulled in Laravel's illuminate-slash-html package, and you can see it right here. Using link_to_route Helper 2:53Do you remember when we worked on the edit page, we pulled in Laravel's illuminate-slash-html package, and you can see it right here. Well, when we did that, we got access to a form facade and its underlying form builder class. Also, an HTML facade and the HTML builder. But finally, you get this helpers.php file that has a bunch of various functions that will help you with common tasks in your views. For example, if you want to very quickly link to a page, there's a link to function. Or if you want to link to an asset, or a named route, or a controller action, all of these will be really useful.Or if you want to link to an asset, or a named route, or a controller action, all of these will be really useful. So why don't we try to use this in our project? Rather than writing all of this, I'm just going to say link to, and yes, we could give it the URL, but remember, we want a named route. So I will say link to route. That has the name of song path. And next, we can set what the actual text of the anchor will be, paste that in. And then once again, the array of attributes. And that's it.And then once again, the array of attributes. And that's it. So once again, we get the exact same end result. And just to prove it to you, that will still work. But now we have a clean way to create our links. Or as another example, when we view a song, maybe there's some kind of navigation somewhere that will link back to the homepage. Well, that's really easy. Right here, how about right at the bottom, we will say link to route, song's path, and the text will be go back home. Resource Route Names 4:25Right here, how about right at the bottom, we will say link to route, song's path, and the text will be go back home. So come back, refresh. At the bottom, we have our link. And now we're back to the main collection. So now you might be wondering how we deal with this when using route resource. Right now, we're hard coding these. But let's delete that and bring back our resource. And now let's see what phpArtisan route list will spit out. Okay, we do get names by default.And now let's see what phpArtisan route list will spit out. Okay, we do get names by default. And notice that they will just mimic the controller method name. So as an example, to show a song, your route name would be songs.show, and that would trigger a show method on the controller. Now it's useful that these are included, but there is a downside. Let me show you. If we were to go back to the index view, let's update this to use the new convention, songs.show. And then if we go into the show view, at the bottom, we can link to songs.index to go back to the collection.And then if we go into the show view, at the bottom, we can link to songs.index to go back to the collection. So yes, this will still work. If I refresh, we can click through. Everything seems fine. And really, if you want to do this, no big problem. However, one thing to just keep in mind is what if within your routes file, for whatever reason, you end up having to change the resource name. As an example, maybe we want to precede it with artist. That way, for example, you could do a URL like example.com slash the artist or band Custom Names for Resources 5:49As an example, maybe we want to precede it with artist. That way, for example, you could do a URL like example.com slash the artist or band name, then songs, and then finally, the name of the song. Well, here's the problem. If we do that, and we run route list again, notice that all of the names have changed. So this sort of breaks our idea, right? That the route name will remain the same even if the URL changes. So in these situations, if this is something that you decide you want to protect yourself against, you can be explicit about the names you provide. So as an example, I can say names will be an array.against, you can be explicit about the names you provide. So as an example, I can say names will be an array. And for the index method on the controller, I want to call that songs path, like we did before. Next, for the show method, that will be song path. So now, if we come back and run that again, we've updated those. But also don't forget, if you're not using all these routes, then remove them. And we learned about this in the last lesson. We can just say only index and show. So if we run it again, now we just get those two routes, but we've set our own route names.We can just say only index and show. So if we run it again, now we just get those two routes, but we've set our own route names. So this means, down the line, when they decide to go back to just songs, well, those names are not going to change. If we run it again, we've updated the URIs, but the route names remain the same. Okay, so I think that does it. That's been your crash course in route naming. Just remember, if you're using a resource, you can use the names option here. Or for a regular URL, we can say foo, and then you can say name this as foo, and have that use this controller.Or for a regular URL, we can say foo, and then you can say name this as foo, and have that use this controller. Finally, within your views, you have the route function, and that will return a URL to a named route. And then you also have, if you pull in that illuminate slash HTML package, a link to route function that you can use. Alright, have fun, and I'll see you in the next video.