Laravel Named Routes

Laravel Named Routes

Named routes allow you to generate URLs or redirect to routes using their name rather than their URI, making your application easier to maintain.

1 - Naming a Route

Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
Route::get('/profile/{user}', [ProfileController::class, 'show'])->name('profile.show');

2 - Generating URLs from Named Routes

// In PHP
$url = route('dashboard');               // https://example.com/dashboard
$url = route('profile.show', ['user' => 42]); // .../profile/42

// In Blade templates
<a href="{{ route('dashboard') }}">Dashboard</a>
<a href="{{ route('profile.show', $user) }}">View Profile</a>

3 - Redirecting to Named Routes

return redirect()->route('dashboard');
return redirect()->route('profile.show', ['user' => $user->id]);

4 - Checking the Current Route

use Illuminate\Support\Facades\Route;

if (Route::currentRouteName() === 'dashboard') {
    // active state logic
}

// In Blade
@if (request()->routeIs('dashboard'))
    <span class="active">Dashboard</span>
@endif