Routes
- Resource Routes
- Redirect Route
- Define a Route With Optional Parameters
- Prefix and Namespace
- Middleware Groups
- Route Existence
- Route-Based Conditional Checks
Resource Routes
Define a Resource Route
Route::resource('posts', PostController::class);
Limit the Routes Created by a Resource Controller (Only)
Route::resource('posts', PostController::class)->only(['index', 'show']);
Exclude Resource Routes (Except)
Route::resource('posts', PostController::class)->except(['destroy', 'update']);
Assign Custom Names to Resource Routes
Route::resource('posts', PostController::class)->names([
'create' => 'posts.build'
]);
Redirect Route
Route::redirect('/', 'dashboard');
Define a Route With Optional Parameters
Route::get('user/{name?}', UserController::class);
Prefix and Namespace
Group routes with a common prefix and name:
Route::prefix('admin')->name('admin.')->group(function () {
Route::get('dashboard', [DashboardController::class, 'index'])->name('dashboard');
Route::get('users', [UserController::class, 'index'])->name('users');
});
Middleware Groups
Route::middleware(['auth', 'verified'])->group(function () {
Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
Route::get('/profile', [ProfileController::class, 'show'])->name('profile');
});
Route Existence
Check If a Route Exists
Route::has('users.edit')
Route-Based Conditional Checks
Check the Current Route Name
Use request()->routeIs()
to check if the current route matches a specific name. This is useful for
applying conditional logic in Blade templates, controllers, or global configurations.
Example: Conditional Content in Blade
@if (request()->routeIs('admin.course.show'))
<p>You are viewing the admin course page.</p>
@endif
Example: Active State in Navigation Menus
<li class="{{ request()->routeIs('admin.course.show') ? 'active' : '' }}">
<a href="{{ route('admin.course.show', $course) }}">View Course</a>
</li>