Kritim Yantra
Jan 08, 2026
You opened a Laravel 12 project, followed an older tutorial, and it said:
“Open app/Http/Kernel.php…”
You looked around.
Nothing.
No Kernel.php.
No $middlewareGroups.
At that moment, every beginner thinks:
“Did I install Laravel wrong?”
You didn’t. Laravel 12 just changed where middleware is registered. And honestly? Once you understand it, the new way is actually cleaner.
Let’s walk through Laravel middleware in 2026, using Laravel 12, in simple terms, with real-world examples, and zero confusion.
Even in Laravel 12, the concept of middleware hasn’t changed.
Middleware is a checkpoint that every request must pass through.
Imagine entering an office building:
Middleware sits between the user and your application logic.
app/Http/Kernel.phpweb and api lived thereKernel.phpbootstrap/app.php
This is the biggest reason beginners get stuck in 2026.
Custom middleware still lives here:
app/Http/Middleware
That part did not change.
Open:
bootstrap/app.php
You’ll see something like this (simplified):
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Middleware;
return Application::configure(basePath: dirname(__DIR__))
->withMiddleware(function (Middleware $middleware) {
// Global middleware
$middleware->append([
// Example:
// \App\Http\Middleware\TrustProxies::class,
]);
// Middleware aliases (replaces Kernel.php)
$middleware->alias([
'auth' => \App\Http\Middleware\Authenticate::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
]);
})
->create();
This file replaces what Kernel.php used to do.
These middleware run for every page, every request.
Security scanners at the entrance of a mall — everyone goes through them.
$middleware->append([
\App\Http\Middleware\TrimStrings::class,
]);
This is what beginners use most.
Example:
Route::get('/dashboard', function () {
return view('dashboard');
})->middleware('auth');
Meaning:
“Only logged-in users can access this page.”
You need a membership card to enter a gym.
auth – Are you logged in?->middleware('auth')
If not logged in → redirected to login page.
guest – Logged-in users should NOT be here->middleware('guest')
Used for:
You don’t show a “Create Account” form to someone who already has one.
throttle – Slow people down->middleware('throttle:60,1')
Limits requests to 60 per minute.
A ticket counter that only serves one person at a time to avoid chaos.
This is where middleware really shines.
php artisan make:middleware EnsureUserIsAdmin
Laravel creates:
app/Http/Middleware/EnsureUserIsAdmin.php
public function handle($request, Closure $next)
{
if (!auth()->check() || !auth()->user()->is_admin) {
abort(403);
}
return $next($request);
}
What this does:
Only managers can enter the control room.
In bootstrap/app.php:
$middleware->alias([
'admin' => \App\Http\Middleware\EnsureUserIsAdmin::class,
]);
This replaces $routeMiddleware from old Laravel.
Route::get('/admin', function () {
return 'Admin Panel';
})->middleware('admin');
Clean. Simple. Reusable.
Laravel still uses groups, but they are defined differently.
You don’t usually edit them directly as a beginner — Laravel already sets them up internally.
Conceptually:
Same company, different rules:
You can still apply middleware in groups like:
Route::middleware(['auth', 'admin'])->group(function () {
Route::get('/settings', fn () => 'Settings');
Route::get('/users', fn () => 'Users');
});
if (!auth()->check()) {
return redirect('/login');
}
->middleware('auth')
If you repeat the same if statement more than twice, middleware is your friend.
Some middleware depends on:
If they run too early, things break silently.
Rule of thumb:
If your middleware needs auth()->user(), it must run after session middleware.
Laravel handles this for most cases—but it’s good to know when debugging weird issues.
Middleware is perfect for:
If it’s about access control, middleware is usually the right tool.
bootstrap/app.phpKernel.php no longer existsOnce middleware clicks, Laravel suddenly feels much easier to manage.
Kernel.php?To simplify bootstrapping and centralize configuration. It reduces magic and makes the app lifecycle clearer.
Yes:
->middleware(['auth', 'admin'])
Absolutely. Middleware prevents bad habits like duplicating checks inside controllers.
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google
Kritim Yantra
Kritim Yantra