Laravel middleware is a mechanism to filter the http request and response. As the name implies that middleware so it’s middle between the request and response.
we can change or validate request and response using the Laravel middleware. There is so many use cases of middleware like authentication, validating token, change request paras, validating a request, validating csrf etc.
Creating a middleware
Creating a middleware is easy using make:middleware
artisan command or we can create manually
Artisan command for make middleware
php artisan make:middleware middleware_name
Now, let’s create a middleware for our application
php artisan make:middleware CheckBlacklist
Above command will create a middleware CheckBlacklist in folder \app\http\Middleware
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class CheckBlacklist
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
if (auth()->user()->is_blacklisted == 1) {
return response()->json('Your account is blacklisted by admin');
}
return $next($request);
}
}
Here, we checked user is blacklisted by admin or not, if user is blacklisted then return the response with message.
Registering middleware
We have created middleware but not registered yet, so to register our new middleware with our Laravel application we need to register it in app/http/kernel.php
Registering middleware globally
To register the middleware globally we need to assign it to $routeMiddleware
property of app/Http/Kernel.php
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
....
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
protected $middleware = [
....
'checkBlacklist' => \App\Http\Middleware\CheckBlacklist::class,
];
}
Middleware to specific routes
We can also assign routes to specific routes using $routeMiddleware
property of app/Http/Kernel.php
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
....
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
protected $routeMiddleware = [
....
'checkBlacklist' => \App\Http\Middleware\CheckBlacklist::class,
];
}
Attaching Middleware to route
Route::get('/create-post',[PostController::class, 'create'])->middleware(CheckBlacklist::class);
Multiple middleware to route
Route::get('/create-post',[PostController::class, 'create'])
->middleware([CheckBlacklist::class,SecoundMiddleware::class]);
Middleware to group of routes
Route::group(['middleware' =>CheckBlacklist::class ], function () {
//
});
Parametrized Middleware
In some cases we need to pass the pass the param to middleware to check some specific conditions like user role for page. We just need to add a extra param to handle
function.
Take a quick example, here $role
is param
public function handle($request, Closure $next, $role)
{
if (! $request->user()->hasRole($role)) {
// Redirect...
}
return $next($request);
}
Ataching to the paramo to route
Route::post('edit-post/{id}', ['middleware' => 'role:editor', function ($id) {
//
}]);