Laravel Basic
Laravel Form
Laravel Database
Laravel Advance
In Laravel, Middleware acts as a bridge between the request and the response.
Every HTTP request entering your Laravel app passes through middleware before reaching controllers/routes.
For example:
Middleware helps you to:
Without middleware, you would have to repeat the same code in every controller – ❌ not good for maintainability.
Use Artisan to create middleware:
php artisan make:middleware EnsureTokenIsValid
This creates app/Http/Middleware/EnsureTokenIsValid.php
.
class EnsureTokenIsValid { public function handle(Request $request, Closure $next): Response { if($request->token !== 'my-secret-token'){ abort(403, 'Unauthorized action.'); } return $next($request); } }
use App\Http\Middleware\EnsureTokenIsValid; return Application::configure(basePath: dirname(__DIR__)) ->withRouting( web: __DIR__.'/../routes/web.php', commands: __DIR__.'/../routes/console.php', health: '/up', ) ->withMiddleware(function (Middleware $middleware): void { $middleware->alias([ 'check.token' => EnsureTokenIsValid::class, ]); }) ->withExceptions(function (Exceptions $exceptions): void { // })->create();
Route::get('/welcome', function(){ return "welcome"; })->middleware('check.token');
Output with my-secret-token:
Output without my-secret-token:
Laravel provides different kinds of middleware:
TrimStrings
, ConvertEmptyStringsToNull
.// bootstrap/app.php ->withMiddleware(function ($middleware) { $middleware->append(\App\Http\Middleware\CheckForMaintenanceMode::class); });
// bootstrap/app.php ->withMiddleware(function ($middleware) { $middleware->alias([ 'check.token' => \App\Http\Middleware\EnsureTokenIsValid::class, ]); }); // routes/web.php Route::get('/dashboard', function () { return "Dashboard"; })->middleware('check.token');
web
, api
groups already exist in Laravel.// bootstrap/app.php ->withMiddleware(function ($middleware) { $middleware->web(append: [\App\Http\Middleware\ExampleWebMiddleware::class]); $middleware->api(prepend: [\App\Http\Middleware\ExampleApiMiddleware::class]); }); // routes/web.php Route::middleware('web')->group(function () { Route::get('/profile', fn() => "Profile Page"); });
// Middleware public function handle($request, Closure $next, $role) { if (! $request->user() || $request->user()->role !== $role) { return redirect('/unauthorized'); } return $next($request); } // Route Route::get('/admin', fn() => "Admin Panel")->middleware('check.role:admin');
public function handle($request, Closure $next) { return $next($request); } public function terminate($request, $response) { \Log::info("Request ended at ".now()); }
Unlike Laravel 10/11 where you had Kernel.php
, in Laravel 12 middleware is configured inside bootstrap/app.php
:
return Application::configure(basePath: dirname(__DIR__)) ->withMiddleware(function ($middleware) { // Register global middleware $middleware->append(\App\Http\Middleware\TrimStrings::class); // Register aliases $middleware->alias([ 'auth.check' => \App\Http\Middleware\AuthCheck::class, ]); // Add to groups $middleware->web(append: [\App\Http\Middleware\ExampleWebMiddleware::class]); // Set priority order $middleware->priority([ \App\Http\Middleware\ImportantFirst::class, \App\Http\Middleware\Second::class, ]); }) ->withRouting( web: __DIR__.'/../routes/web.php', api: __DIR__.'/../routes/api.php', ) ->create();
/dashboard
./admin
.