Mahamudul Hasan Rubel
HomeAboutProjectsSkillsExperienceBlogPhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • About
  • Projects
  • Skills
  • Experience
  • Blog
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
LaravelPHPJune 20, 20264 min read

Laravel Middleware: A Practical Guide to Request Filtering

Master Laravel middleware to clean up your controllers and handle request filtering efficiently. Learn the PHP request lifecycle in this hands-on guide.

laravelphpmiddlewarebackendweb developmentTutorial
A close-up of a stop button on a public bus, highlighting travel and safety features.

Last month, I was debugging a messy controller that had grown to nearly 400 lines because it checked for user permissions, logged access times, and validated headers before doing any real work. I realized I was fighting the framework instead of using it; moving that logic into custom request filters saved me hours of maintenance.

Understanding the PHP request lifecycle

Before you dive into writing code, you need to visualize how a request travels through your application. When a user hits your site, the request doesn't just jump straight into your controller. It passes through a series of "layers"—this is your laravel middleware.

Think of it like a security checkpoint at an airport. You have to pass through baggage claim, the metal detector, and document verification before you ever step onto the plane. If you fail any of these, you’re turned away. In Laravel, these checkpoints can inspect, reject, or modify the incoming request before it reaches your application logic.

If you are still struggling with the basics of how traffic gets routed, it’s worth brushing up on Laravel routing and controllers: A Beginner's Guide to MVC before getting too deep into the filtering logic.

Why you need custom middleware

I’ve seen junior devs put if statements at the top of every single controller method to check for a specific header or role. It’s a nightmare to test and even harder to change later. When you use laravel middleware, you centralize that logic.

We once tried to use a global BaseController to handle authentication checks, but it quickly became a mess of inheritance. Moving to middleware allowed us to attach the logic only to the routes that actually needed it, keeping the rest of the application lean.

Building your first filter

A modern minimalist photo of two black circular filters on contrasting paper for design and technology concepts.

Let’s say you want to ensure a request has a specific "Secret-Header" before it can access your API. First, create the class using Artisan:

Bash
php artisan make:middleware EnsureSecretHeader

Inside app/Http/Middleware/EnsureSecretHeader.php, you’ll see a handle method. This is where the magic happens:

PHP
public function handle(Request $request, Closure $next)
{
    if ($request->header('Secret-Header') !== 'my-secret-key') {
        return response()->json(['message' => 'Unauthorized'], 403);
    }

    return $next($request);
}

The $next($request) call is the most important part—it passes the request to the next middleware or your controller. If you don't call it, the request dies right there. Once you’ve written your logic, register it in bootstrap/app.php (for Laravel 11+) or app/Http/Kernel.php (for older versions).

Organizing your web development fundamentals

Once you start using middleware, you'll see why it's a pillar of web development fundamentals. You aren't just filtering; you're creating a robust, predictable flow for your data.

  1. Global Middleware: Runs on every single request. Use this for things like CORS or maintenance mode.
  2. Route Middleware: Assigned to specific routes or groups. This is where you’ll handle authentication or role-based access.

If you find your middleware is getting complicated, check if you are trying to do too much. For example, if you are checking if a user has filled out their profile, ensure you aren't duplicating Form validation in Laravel made easy: A Practical Guide inside your filter. Middleware should handle the request state, while FormRequests handle the data integrity.

Common pitfalls to avoid

The most common mistake I see is developers performing heavy database queries inside middleware. Remember, this code runs on every request you target. If your middleware takes 200ms to run, your entire application just slowed down by 200ms for every user. Keep it light.

Also, don't ignore the 7 Laravel errors every beginner hits (and how to fix them). I’ve spent way too long debugging a middleware that wasn't firing simply because I forgot to register it in the correct group. Always check your service provider or kernel configuration first.

FAQ: Frequently Asked Questions

Close-up of a magnifying glass focusing on the phrase 'Frequently Asked Questions'.

Can I modify the request in middleware? Yes. You can add attributes to the request object using $request->merge(['new_data' => 'value']) before passing it to $next.

What is the difference between middleware and a controller? Middleware is for cross-cutting concerns (logging, auth, headers) that apply to many routes. Controllers are for the specific business logic of a single endpoint.

Does order matter? Absolutely. Middleware runs in the order you define it. If you have an authentication middleware and a logging middleware, you usually want the authentication to run first so you know who is making the request before you log it.

Middleware is one of those tools that feels like "magic" until you write a few yourself. Start by moving one simple if check out of a controller and into a middleware class. You’ll be surprised how much cleaner your code looks. I’m still refining how I group my middleware for larger projects, and I’m currently experimenting with more descriptive naming conventions for my middleware stacks. Don't be afraid to refactor as your app grows.

Back to Blog

Similar Posts

Notebook labeled 'Mistake' next to a red delete eraser on a dark background.
LaravelPHPJune 20, 20264 min read

7 Laravel errors every beginner hits (and how to fix them)

7 Laravel errors every beginner hits? Don't panic. Learn how to fix common routing, Eloquent, and validation mistakes to speed up your development process.

Read more
A flock of birds flying in V formation against a clear blue sky.
LaravelPHPJune 20, 20264 min read

Understanding migrations and seeders in Laravel for beginners

Understanding migrations and seeders is essential for managing Laravel database schemas. Learn how to version control your data structure and seed demo records.

Read more
Close-up of a person signing a document on a wooden table, emphasizing detail and focus.
LaravelPHPJune 20, 20263 min read

Form validation in Laravel made easy: A Practical Guide

Form validation in Laravel is simple when you move logic out of your controllers. Learn how to use FormRequest classes to keep your code clean and dry.

Read more