1Route::middleware([CheckAge::class])->group(function () {
2 Route::get('/', function () {
3 //
4 });
5
6 Route::get('admin/profile', function () {
7 //
8 })->withoutMiddleware([CheckAge::class]);
9});
1Route::middleware(['first', 'second'])->group(function () {
2 Route::get('/', function () {
3 // Uses first & second Middleware
4 });
5
6 Route::get('user/profile', function () {
7 // Uses first & second Middleware
8 });
9});
1<?php
2
3namespace App\Http\Middleware;
4
5use Closure;
6
7class CheckAge
8{
9 /**
10 * Handle an incoming request.
11 *
12 * @param \Illuminate\Http\Request $request
13 * @param \Closure $next
14 * @return mixed
15 */
16 public function handle($request, Closure $next)
17 {
18 if ($request->age <= 200) {
19 return redirect('home');
20 }
21
22 return $next($request);
23 }
24}