Middleware in Laravel allows you to filter HTTP requests entering your application. It provides a convenient mechanism to perform actions before a request is handled by the application's route or after the response has been generated. In this post, we'll walk through a step-by-step guide on how to create custom middleware in Laravel, giving you the flexibility to add custom logic to your application's request flow.
Step 1: Create the Middleware
To create custom middleware in Laravel, use the make:middleware
Artisan command:
php artisan make:middleware CustomMiddleware
This will generate a new middleware class file named CustomMiddleware.php
in the app/Http/Middleware
directory.
Step 2: Define the Middleware Logic
Open the CustomMiddleware.php
file and implement the middleware logic in the handle
method. This method will be called for every incoming request that passes through this middleware.
<?php
namespace App\Http\Middleware;
use Closure;
class CustomMiddleware
{
public function handle($request, Closure $next)
{
// Middleware logic goes here
return $next($request);
}
}
Step 3: Register the Middleware
To use the custom middleware, you need to register it in the app/Http/Kernel.php
file. Open the file and add your middleware to the $middleware
array or the $routeMiddleware
array if you want to use it for specific routes.
protected $middleware = [
// Other middleware
\App\Http\Middleware\CustomMiddleware::class,
];
// Or, for route-specific usage
protected $routeMiddleware = [
// Other middleware
'custom' => \App\Http\Middleware\CustomMiddleware::class,
];
Step 4: Applying Middleware to Routes
Now that your custom middleware is registered, you can apply it to routes in your routes/web.php
or routes/api.php
file.
// Apply middleware to individual routes
Route::get('/example', function () {
// Your route logic here
})->middleware('custom');
// Apply middleware to a group of routes
Route::middleware('custom')->group(function () {
// Your routes here
});
Step 5: Middleware Logic
Custom middleware can perform various tasks, such as authentication, authorization, logging, modifying request/response, and more. For example, let's add a simple logging functionality to our custom middleware:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Log;
class CustomMiddleware
{
public function handle($request, Closure $next)
{
// Log the incoming request details
Log::info('Incoming request:', [
'method' => $request->method(),
'path' => $request->path(),
'params' => $request->all(),
]);
return $next($request);
}
}
Step 6: Testing the Custom Middleware
With the custom middleware implemented, run your Laravel application and access the routes where the middleware is applied. You should see the logged request details in your application's log file.
Creating custom middleware in Laravel provides a powerful way to add reusable and flexible logic to your application's request flow. Whether it's authentication, logging, or any other custom functionality, middleware allows you to intercept requests and perform actions before they reach the route handlers.