Cover Image for Laravel Middleware
130 views

Laravel Middleware

Middleware in Laravel is a crucial part of the request handling process. It allows you to filter and modify incoming HTTP requests and outgoing responses before they reach your application’s routes or after they leave your application’s controllers. Middleware is used to perform tasks such as authentication, authorization, logging, request and response modification, and more. Laravel provides several built-in middleware, and you can also create custom middleware to suit your application’s specific needs.

Here’s how you can work with middleware in Laravel:

  1. Built-in Middleware: Laravel includes several built-in middleware for common tasks. Some commonly used built-in middleware include:
  • auth: Checks if the user is authenticated. Redirects to the login page if not.
  • guest: Checks if the user is a guest (not authenticated).
  • throttle: Limits the rate at which certain routes can be accessed to prevent abuse.
  • cors: Handles Cross-Origin Resource Sharing (CORS) requests. You can apply these middleware to routes in your routes/web.php or routes/api.php file:
PHP
 Route::get('/dashboard', 'DashboardController@index')->middleware('auth');
  1. Creating Custom Middleware: You can create custom middleware using the make:middleware Artisan command:
Bash
 php artisan make:middleware CustomMiddleware

This will create a new middleware class in the app/Http/Middleware directory. You can then define custom logic in the handle method of your middleware:

PHP
 public function handle($request, Closure $next)
 {
     // Custom middleware logic here
     return $next($request);
 }
  1. Middleware Groups: You can group multiple middleware together and apply them to routes as a group. This is useful when you want to apply the same set of middleware to multiple routes:
PHP
 Route::middleware(['auth', 'custom'])->group(function () {
     // Routes that use both 'auth' and 'custom' middleware
 });
  1. Middleware Parameters: You can pass parameters to your custom middleware by specifying them when applying the middleware to a route:
PHP
 Route::get('/example', 'ExampleController@index')->middleware('custom:param1,param2');

Then, in your middleware’s handle method, you can access these parameters:

PHP
 public function handle($request, Closure $next, $param1, $param2)
 {
     // Use $param1 and $param2
     return $next($request);
 }
  1. Terminable Middleware: Terminable middleware allows you to perform actions after the response has been sent to the client. To create terminable middleware, you can implement the TerminableMiddleware interface and define the terminate method:
PHP
 public function terminate($request, $response)
 {
     // Perform actions after the response has been sent
 }

Laravel will automatically call the terminate method after the response has been sent.

  1. Global Middleware: Global middleware is executed on every HTTP request to your application. You can specify global middleware in the $middleware property of the app/Http/Kernel.php file:
PHP
 protected $middleware = [
     // ...
     \App\Http\Middleware\CustomGlobalMiddleware::class,
 ];

Middleware in Laravel provides a powerful and flexible way to add functionality to your application’s request handling process. It helps you keep your code organized and allows you to apply various filters and modifications to incoming requests and outgoing responses.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS