Unlimited WordPress themes, graphics, videos & courses! Unlimited asset downloads! From $16.50/m
by
Lessons:9Length:41 minutes
Laravel400
  • Overview
  • Transcript

2.3 Named Routes

Being able to link and redirect to different routes is really important when building a website or web app. Laravel makes it easy to do this with named routes. I'll show you how they work in this lesson.

2.3 Named Routes

So far in this course we've set up a couple of our static pages and things are starting to look pretty good. We could go ahead and create even more static pages if we wanted to. We just need to put them together like we did in the previous lesson. But what do we do when it comes to linking to those pages. At the moment, in our template, we still have dummy links that don't actually go anywhere. So how about we link them up now. To do this, we can simply change our anchors to point to each of the pages that we created. We need to use absolute password so that it references the pages from the document root. There is a problem with doing it like this. Let me explain, what we're to happen if we were to go into routes file and we change the route for our about page to about-me? If we refresh our homepage and then attempt to navigate to our about page we get a 404 error and that's because we've changed the route of our about page to about-me however the link in the anchors still references the old route. Now the thing is, we really shouldn't be changing our routes, especially if its' for a live site, as the search engines like Google might have already indexed the page meaning it will now be a dead link. However, during development things might change and that's acceptable. Not only that but sometimes it's hard to remember what the routes were or what they might be because they might be too long to type out. So what can we do about that? Well, luckily for us Laravel has what's called named routes and they are pretty much exactly what they sound like. When we set up our route definitions, we can set a name on each of the routes and then we can simply refer to that name to have Laravel generate the appropriate link for us. How about we take a look at what I mean, because it will make more sense. For our about route, we first need to route our closure in an array. Then, before the closure, we add an as key. And for the value, we set it to about. This is now the name of our route. So, looking at this route, we have a get request that responds to, about-us and is named, about. The closure will be called and our about view will be returned as the response. So now that have named our route, we need to be able to use this name in our template view. So inside the anchors we'll use the {{ syntax to echo out some PHP. And inside we're going to use the route helper function. All we need to do for this function is passing the name of our route. For us that's going to be about and that's it. If we now refresh our homepage and hover over the link, we can see that it now points to the about-us page. If we now go back to our web routes file and we change the route back to just about, we no longer need to worry about changing any references to it that are using the same name. Laravel will automatically generate this URL for us based on its name. So, just for consistency's sake let's, once again, refresh our browser and we can see that the link is indeed still pointing to the correct route. So how about we give our homepage a name as well. In our web routes file we'll wrap our closure in an array, and then we'll define the name of the route. We'll name this one Home. Then in our template view we can link it up just like we did with the About page. We'll use the route function with Home as the name we wish to use. Now I know that we haven't looked at route parameters in this course, nor will we. But named routes can handle those as well and it's a cinch. We just pass in an array of parameters as a second argument to the route function. And that just about covers named routes and just how handy they are. I strongly recommend that you get used to using them. Start naming your routes and remember, it's sometimes useful to namespace your names by using dots like this. For a route that creates a new user, you might name it something like users.create. We've now covered all of the static side of our website, and it's coming together really well. Next up we're going to start looking at the dynamic content, and this is going to be the building of our contact form.

Back to the top