Mahmoud Al-HabashOpen to new projects
  • WorkWork
  • ServicesServices
  • LabLab
  • WritingWriting
  • ContactContact
Amman--:--عربي
Writing/Laravel

Laravel Middleware: Creating Custom Guards

Protect your routes with custom middleware and authentication guards.

24 December 20251 min readLaravelاقرأ بالعربية

On this page

  1. Basic Middleware
  2. Middleware with Parameters
  3. Register Middleware

Create custom middleware:

Basic Middleware

php
public function handle($request, Closure $next)
{
    if (!$request->user()->isAdmin()) {
        abort(403);
    }
    return $next($request);
}

Middleware with Parameters

php
public function handle($request, Closure $next, $role)
{
    if (!$request->user()->hasRole($role)) {
        abort(403);
    }
    return $next($request);
}

Register Middleware

php
protected $routeMiddleware = [
    'admin' => \App\Http\Middleware\AdminMiddleware::class,
];
All articles

Keep reading

  • Laravel Action Classes Pattern16 January 20261 min read
  • Laravel Action Classes: Single Responsibility16 January 20261 min read
  • Laravel API Rate Limiting16 January 20261 min read