Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Routing  ›  Capstone Project

Named Routes

Named routes 15 minNaming a route lets code build its URL with route() and redirect to it by name, so URIs can change without breaking every link.

Named Routes

Lendly's book page is linked from the home page, search results, a member's loan history, emails and more. If those links are typed as "/books/" . $id, then moving the page to /library/{id} means hunting down every one. Named routes let you link to the route, not the address.

The rule

Name the route once; build every URL with route().

Give the route a name:

Route::get('/books/{id}', function (string $id) {
    return view('books.show', ['id' => $id]);
})->name('books.show');

Then generate its URL anywhere with the route() helper, passing parameters as an array:

route('books.show', ['id' => 7]);   // http://lendly.test/books/7

In Blade:

<a href="{{ route('books.show', ['id' => $book->id]) }}">{{ $book->title }}</a>

Redirects use the name too:

return redirect()->route('books.show', ['id' => 7]);
return to_route('books.show', ['id' => 7]);   // same thing, shorter

A few details:

  • route() returns a full URL, including the domain from APP_URL or the current request. Pass false as the third argument for just the path: route('books.show', ['id' => 7], false) gives /books/7.
  • Extra array keys that are not parameters become the query string: route('books.index', ['page' => 2]) gives /books?page=2.
  • Names must be unique. The convention is resource.action: books.index, books.show, loans.store.
  • Asking for a name that does not exist throws an error, so typos are caught at once instead of producing dead links.
  • request()->routeIs('books.*') checks whether the current page belongs to a name pattern, which is handy for highlighting menu items.

The shortcut

Treat the URI as a private detail of the routes file. Everywhere else, templates, redirects and emails, speak only in names. Then a URL change is a one-line edit.

Moving the book pageStep 1 of 4
  1. Before

    Route::get('/books/{id}', ...)->name('books.show'), and 20 templates call route('books.show', ...).

The trap is naming routes and then still hard-coding href="/books/7" in a template. That link breaks on the next move.

Drills

Answer fast — trust the rule.

quick drill

How do you give a route the name loans.index?

quick drill

A route is /books/{id} named books.show. What does route('books.show', ['id' => 4], false) return?

quick drill

What does route('books.index', ['page' => 3]) give if books.index is /books with no parameters?

quick drill

You change a named route's URI. What else must you edit for links built with route()?

quick drill

Which line redirects to a named route after saving?

Remember

  • Name the route once; build every URL with route().
  • The URI is private to the routes file. Everything else speaks in names.
  • Trap: a hard-coded href defeats the name and breaks on the next move.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainRename the URL, keep the links

Lendly links to its book detail page from twenty templates. Explain how naming the route and using route() protects those links if /books/{id} later becomes /library/{id}. Include the route definition and one link.