What are Named Routes?

In Laravel, Named Routes are a way to give a unique name to a route.

Instead of writing the full URL everywhere, you can just use the route’s name.

This makes your code cleaner, easier to maintain, and less error-prone.

Defining a Named Route

Route::get('/user/profile', function () {
    return 'User Profile';
})->name('profile');

Here, the route’s name is profile.


Using Named Routes

You can create URLs for named routes like this:

$url = route('profile');
URL:
/user/profile

Redirecting with Named Routes

return redirect()->route('profile');

Why Use Named Routes?

  • If the URL changes, you only update it in one place.
  • Code looks clean and readable.
  • Useful for large projects with many routes.

Named Routes with Parameters

Route::get('/user/{id}/profile', function ($id) {
    return "Profile of user: " . $id;
})->name('user.profile');
$url = route('user.profile', ['id' => 5]);
URL:
/user/5/profile
Comments