Lessons: 21Length: 2.4 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

3.3 Generating URLs for Routes

In the previous lesson, we looked at the URL function and how it can foolproof the URLs for our applications resources. I mean, we don't have to think about it. We just pass in the relative URL to that URL function and we're good to go. It would be nice if we could do the same thing for our routes. And we could I mean, technically we could use the URL function. So for our home, we could say URL, and then use the slash there and then that would link to our Homepage or the welcome page. But I want something a little more robust because URLs change. They don't change very often, but they do change. And I would like the application to adapt to those changes. So that if we ever just change the url of our route, like we did for the about, then that would just automatically be reflected in other parts of the application. And we have that capability instead of using URL, there's another function called route. But we don't pass in a URL, we pass in the name of that route. So let's first of all look at how we can name our routes and it's very simple. All we have to do is chain a call to the name method whenever we define a route. So here we are setting up the route for the get request for the index on our home controller. So the name in this case could be index, but as you'll see as you add more controllers you're gonna have a lot of indexes. So it makes a lot of sense to use the name of the controller and then the name of the action on that controller. And I use the dot in between those two values, so that we will have home index and then we can essentially do the same thing for about and contact then we just need to make the necessary changes. And there we go. We have named our routes, they are unique. That's very important. Our routes need to have unique names. And so now we can use those names to generate the URLs. So for the Homepage that was home index, then let's just copy and paste. And then we will make the necessary changes for the about routes and then the contact route. So let's go back to the browser. Let's refresh. Now we know that the about is different because we change that in the previous lesson, it is /about/about. So let's inspect this and we can look at the URL that was generated. And there it is, /about/about. So if we click on this, it's going to automatically take us to the about page. That's great. Of course, if we click on Home, we see the index view. So let's change our URL back to just /about because that's what we want anyway, then all we have to do is just refresh the page. If we look at the about link, we can see that the URL is now updated. We don't have to worry about that at all. So as you are linking to other routes makes a lot of sense to name your routes so that you can use the route function to generate the URL for those routes. It takes just a few seconds, but it saves you a whole lot of headache later on.

Back to the top