Laravel 11 has established itself as one of the most powerful tools for web application development in PHP. Among its many features, middleware allows filtering of HTTP requests that come to the application. In this guide, we will explore how to create custom middleware in Laravel 11 easily and quickly, providing developers with the ability to extend the functionalities of their applications without complications.
What is Middleware in Laravel?
Middleware are layers of code that run before or after an HTTP request is handled by the application. Their main purpose is to intercept requests and responses, allowing tasks such as authentication, authorization, role verification, or even activity logging. In Laravel, middleware can be used to add additional logic to applications.
Creating Custom Middleware
Step 1: Generate the Middleware
To start, you need to generate a new middleware using Artisan, Laravel's command-line interface. This can be done by running the following command in the terminal:
php artisan make:middleware MiddlewareName
Replace MiddlewareName
with the desired name for your middleware. This command will generate a new file in app/Http/Middleware
.
Step 2: Define the Middleware Logic
Once the middleware has been created, it's time to define its logic. Open the generated file and look for the handle
method. This method accepts three parameters: the request, the next response, and the closure. This is where you can implement the filtering logic you want:
public function handle(Request $request, Closure $next)
{
// Custom logic
// Example: check if the user is authenticated
if (!auth()->check()) {
return redirect('login');
}
return $next($request);
}
In this example, the middleware checks if the user is authenticated; if not, it redirects them to the login page.
Step 3: Registering the Middleware
After defining the middleware logic, it's necessary to register it in the application. This is done in the app/Http/Kernel.php
file. There, you can add the middleware in the corresponding section (global or specific groups) as needed.
protected $routeMiddleware = [
// other middlewares
'middlewareName' => \App\Http\Middleware\MiddlewareName::class,
];
Step 4: Using the Middleware
Finally, to utilize the middleware that has been created, it can be applied to specific routes in the Laravel route files (web.php
or api.php
). This is done by adding the middleware as part of the route definition:
Route::get('/protected-route', 'Controller@method')->middleware('middlewareName');
This way, all requests to /protected-route
will pass through the middleware, allowing the execution of the previously defined logic.
Conclusion
Creating custom middleware in Laravel 11 is a straightforward process that allows developers to add layers of logic to their applications. With just a few steps, it is possible to efficiently manage requests and responses, thus enhancing the security and functionality of the application.
For more relevant content on Laravel and web development, I invite you to continue exploring my blog where you will find more updates and practical guides.