What are named routes in Laravel?
Named routes allow you to assign a unique name to a route, making it easier to reference throughout your application without hard-coding URLs.
Basic Syntax
// Instead of this:
Route::get('posts', [PostController::class, 'index']);
// Use named route:
Route::get('posts', [PostController::class, 'index'])->name('posts.index');
Examples of Named Routes
1. Basic Named Route
// routes/web.php
Route::get('/about', function () {
return view('about');
})->name('about.page');
2. Named Route with Controller
Route::get('/users', [UserController::class, 'index'])->name('users.index');
Route::get('/users/{id}', [UserController::class, 'show'])->name('users.show');
3. Named Route with Parameters
Route::get('/posts/{post}/comments/{comment}', function ($postId, $commentId) {
// ...
})->name('posts.comments.show');
Generating URLs Using Named Routes
In Controllers:
public function store(Request $request)
{
// Redirect using named route
return redirect()->route('posts.index');
// With parameters
return redirect()->route('users.show', ['id' => 1]);
// With multiple parameters
return redirect()->route('posts.comments.show', [
'post' => 5,
'comment' => 12
]);
}
In Blade Templates:
<!-- Basic usage -->
<a href="{{ route('about.page') }}">About Us</a>
<!-- With parameters -->
<a href="{{ route('users.show', ['id' => $user->id]) }}">
View Profile
</a>
<!-- With multiple parameters -->
<a href="{{ route('posts.comments.show', [
'post' => $post->id,
'comment' => $comment->id
]) }}">
View Comment
</a>
In API Responses:
return response()->json([
'user_profile_url' => route('users.show', ['id' => $user->id])
]);
Benefits of Named Routes
1. Maintainability
If you change the URL structure, you only need to update it in one place:
// Before: /posts
// After: /articles
Route::get('/articles', [PostController::class, 'index'])->name('posts.index');
// All route('posts.index') calls will now point to /articles
2. Readability
// Much clearer than:
return redirect('/users/' . $id);
// Use:
return redirect()->route('users.show', ['id' => $id]);
3. Parameter Handling
// Automatic parameter encoding
$url = route('users.show', ['id' => 'john doe']);
// Result: /users/john%20doe
Advanced Examples
Route Groups with Names
Route::name('admin.')->prefix('admin')->group(function () {
Route::get('/dashboard', function () {
// Named: admin.dashboard
})->name('dashboard');
Route::get('/users', function () {
// Named: admin.users
})->name('users');
});
// Usage: route('admin.dashboard')
Optional Parameters
Route::get('/products/{category?}', function ($category = null) {
// ...
})->name('products.list');
// Both work:
route('products.list'); // /products
route('products.list', ['category' => 'electronics']); // /products/electronics
Checking Current Route
// In controllers or middleware
if (request()->route()->named('admin.*')) {
// Apply admin-specific logic
}
// In Blade templates
@if (Route::is('about.page'))
<p>You're on the about page</p>
@endif
Best Practices
-
Use consistent naming conventions:
// CRUD operations Route::get('/posts', [PostController::class, 'index'])->name('posts.index'); Route::get('/posts/create', [PostController::class, 'create'])->name('posts.create'); Route::post('/posts', [PostController::class, 'store'])->name('posts.store'); Route::get('/posts/{id}', [PostController::class, 'show'])->name('posts.show'); Route::get('/posts/{id}/edit', [PostController::class, 'edit'])->name('posts.edit'); Route::put('/posts/{id}', [PostController::class, 'update'])->name('posts.update'); Route::delete('/posts/{id}', [PostController::class, 'destroy'])->name('posts.destroy');
-
Use route names instead of URLs everywhere in your application
-
Group related routes with prefixes:
Route::name('api.')->prefix('api/v1')->group(function () { Route::get('/users', [Api\UserController::class, 'index'])->name('users.index'); });
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Terms Disclaimer About Us Contact Us
Copyright 2023-2025 © All rights reserved.