In this tutorial, I will show you how to exclude routes from Laravel middleware. We know that we can add middleware to our route. But sometimes we need to exclude some route for a specific middleware.
In this Laravel exclude middleware from route tutorial, I will show you how we can exclude middleware from the Laravel route. See the below example code:
routes/web.php
<?php
use App\Http\Middleware\EnsureTokenIsValid;
Route::middleware([EnsureTokenIsValid::class])->group(function () {
Route::get('/', function () {
//
});
Route::get('/profile', function () {
//
})->withoutMiddleware([EnsureTokenIsValid::class]);
});
//or
Route::middleware(['check-token'])->group(function () {
Route::get('/', function () {
//
});
Route::get('/profile', function () {
//
})->withoutMiddleware(['check-token']);
});
We can exclude a given set of middleware from an entire group of route definitions as well:
routes/web.php
<?php
use App\Http\Middleware\EnsureTokenIsValid;
Route::withoutMiddleware([EnsureTokenIsValid::class])->group(function () {
Route::get('/profile', function () {
//
});
});
Read also: Complete Explanation With Example On Laravel Middleware
Hope it can help you.