Named Routes & Route URL Generation
Named routes assign unique identifiers to routes so you can generate URLs by name rather than hardcoding paths throughout your codebase. When a URL changes, you update it in one place (the route definition) and every reference updates automatically. This is one of the most important maintenance patterns in Laravel — it's the difference between a fragile app that breaks on URL changes and a maintainable one.
Named Routes in Practice
// Define
Route::get('/posts/{post}', [PostController::class, 'show'])
->name('posts.show');
Route::post('/posts', [PostController::class, 'store'])
->name('posts.store');
// Generate URL
$url = route('posts.show', ['post' => 42]);
// Returns: https://yourdomain.com/posts/42
$url = route('posts.show', $post);
// Laravel auto-extracts the primary key (or route key)
// Check if current route has a name
if (Route::currentRouteName() === 'posts.show') {
// We're on the posts show page
}
// In Blade templates
<a href="{{ route('posts.show', $post) }}">{{ $post->title }}</a>
// Redirect to named route
return redirect()->route('posts.store');
return redirect()->route('posts.show', $post);Naming Conventions
Laravel follows dot-notation naming conventions: resource-name.action. For Post: posts.index, posts.create, posts.store, posts.show, posts.edit, posts.update, posts.destroy. For nested resources: posts.comments.index, posts.comments.store. For admin routes: admin.posts.index. Route::resource() automatically generates these names. The convention makes code instantly readable — route('admin.posts.destroy', $post) communicates exactly what it does without looking up the route definition.
Middleware = filters that run before/after your controller logic
Tip
Tip
Practice Named Routes Route URL Generation in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Practice Task
Note
Practice Task — (1) Write a working example of Named Routes Route URL Generation from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Named Routes Route URL Generation is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready laravel code.