Launch offer
Business website $150 USD Custom plugin $200 USD Ready in 5 days
Get a quote
Laravel

Middleware in Laravel

Filter HTTP requests with middleware for auth, roles, locales, and custom checks.

Middleware in Laravel — a practical guide to Laravel middleware with clear examples you can reuse in real projects.

Laravel Tutorial Series (25/27). Prefer one page? Read the complete Laravel tutorial (all 27 topics).

Short description

Middleware sits between the request and your controller — perfect for authentication, logging, and access rules.

Create middleware

php artisan make:middleware EnsureIsAdmin
public function handle(Request $request, Closure $next)
{
    if (! $request->user()?->is_admin) {
        abort(403);
    }

    return $next($request);
}

Register and use (Laravel 11+ style in bootstrap/app.php or route)

Route::get('/admin', [AdminController::class, 'index'])
    ->middleware(['auth', EnsureIsAdmin::class]);

Group usage

Route::middleware(['auth'])->group(function () {
    Route::get('/dashboard', DashboardController::class);
});

Leave a reply

Your email address will not be published. Required fields are marked *