Could we help you? Please click the banners. We are young and desperately need the money
So you're diving into Laravel and keep hearing the word middleware thrown around? Maybe you've even seen it in route definitions, but you're not entirely sure what it does. Don't worry — in this guide, we’ll break down middleware in Laravel 12 in simple terms, complete with code examples and a real-world use case to get you confident using it in your projects.
In Laravel, middleware acts like a filter or a gatekeeper. It processes incoming HTTP requests before they reach your controller and can also handle responses before they’re sent back to the browser.
You can use middleware to:
If your Laravel app were a club, middleware would be the security guard at the entrance checking IDs, dress codes, or memberships.
Laravel ships with several handy middleware classes out of the box:
These middleware are typically applied globally (to all requests) or to specific routes as needed.
Let’s walk through creating a simple custom middleware in Laravel 12.
php artisan make:middleware BlockAfterSix
Edit "app/Http/Middleware/BlockAfterSix.php" like so:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
class BlockAfterSix
{
public function handle(Request $request, Closure $next): Response
{
if (now()->hour >= 18) {
return response('Sorry, this page is only accessible before 6 PM.', 403);
}
return $next($request);
}
}
Open "bootstrap/app.php" and locate where your app is bootstrapped:
return Application::configure(basePath: dirname(__DIR__))
->withRouting(...)
->withMiddleware(function (Middleware $middleware): void {
// Global middleware
$middleware->append(\App\Http\Middleware\BlockAfterSix::class);
// OR register as route middleware
$middleware->alias([
'block.after.six' => \App\Http\Middleware\BlockAfterSix::class,
]);
})
->create();
Use "append()" for global middleware or "alias()" to apply it selectively via routes.
Route::get('/special-offer', function () {
return view('special-offer');
})->middleware('block.after.six');
Now if someone visits "/special-offer" after 6 PM, they'll get a 403 error.
Here’s what happens when you apply middleware:
You can stack multiple middleware. Laravel will run them in the order they appear in the route group or middleware array.
Apply to individual routes like this:
Route::get('/dashboard', function () {
return view('dashboard');
})->middleware('auth');
If you're using middleware groups, you can define them in "bootstrap/app.php" like so:
$app->middlewareGroup('web', [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Session\Middleware\StartSession::class,
// Other middleware...
]);
This lets you apply multiple middleware to a group of routes:
Route::middleware('web')->group(function () {
Route::get('/', fn() => view('welcome'));
});
Let’s say you want only admins to access the admin panel.
php artisan make:middleware IsAdmin
public function handle(Request $request, Closure $next)
{
if (auth()->check() && auth()->user()->role === 'admin') {
return $next($request);
}
abort(403, 'Access denied');
}
->withMiddleware(function (Middleware $middleware): void {
$middleware->alias([
'is.admin' => \App\Http\Middleware\IsAdmin::class,
]);
});
Route::middleware('is.admin')->group(function () {
Route::get('/admin/dashboard', [AdminController::class, 'dashboard']);
});
Middleware is one of Laravel’s most powerful features for maintaining clean, reusable, and secure code. Whether you're building an e-commerce platform, a portfolio site, or a full-blown SaaS app, understanding middleware will save you a ton of time and keep your codebase sane.
Now that you’ve got the hang of the basics, try creating your own middleware for other use cases — like language localization, geo-blocking, or logging traffic. It’s all about building good habits early as you grow your Laravel skills.