♦️

Middleware

 
There are three types of middleware:
  1. Global Middleware
  1. Grouped Middleware
  1. Routed Middleware
If you want to give permission on routes then we will use middlewares
php artisan make:middleware checkAge

Global Middleware ⇒

 
Once You have created the middleware , To use in your application you have to add inside the app→Http→kernel.php and add under protected $middleware { }
notion image
🔥
Here when you add in the kernel.php then you have to decide either you want to add it under global ,grouped or routed middleware
Now you have done the configuration part of the middleware and you can write the condition of the middleware under app→ Http→middleware→checkAge.php
Here you have to check age variable so you can write like
if($request->age && $request->age<18) { return redirect ("noaccess"); // you have to build noacees route with a view } return $next($request); // you must have to return the request otherwise it will not work
 
 

Grouped Middleware

So before this we have added the middleware url into the global array of middleware but this time we have to add the middleware under the grouped array and associate it with a group name
notion image
Group name can be anything
Now you have to specify which are the pages you want to add this middleware
notion image
here middleware is the method or hardcoded → protectedPage is the name of the middleware and under the function you have to give the urls or views
 

Routed Middleware

we have to change the registry in Kernel.php and add the middleware with a name under route middleware array
notion image
to apply middleware go to web.php where all the routes has been written
Route::view('user','user')->middleware('middleware_name');
notion image