Table of Contents
Introduction:
Middleware is a powerful component in Laravel that allows you to filter HTTP requests that enter your application. It acts as an intermediary between a request and a response, and can be used for various tasks such as authentication, authorization, and data validation.
One of the main uses of middleware is to check for an authenticated user before allowing them to access certain parts of your application. For example, you may have a middleware that checks if a user is logged in before allowing them to access the dashboard of your application. If the authentication is not verified as valid, the middleware can redirect the user to the login page.
Creating Middleware:
In Laravel, creating a new middleware is easy. You can use the following command to create a new middleware:
php artisan make:middleware <middleware_name>
Replace <middleware_name> with the name of your middleware. For example, to create a middleware called CheckUser, you would use the following command:
php artisan make:middleware CheckUser
Once you have created your middleware, it needs to be registered before it can be used. Laravel provides two types of middleware: global middleware and route middleware.
Global middleware:
Global middleware runs during every HTTP request of your application. These middleware can be listed in the $middleware property of the app/Http/Kernel.php class.
Route middleware, on the other hand, is used for specific routes. These middleware can be added to the $routeMiddleware property of the app/Http/Kernel.php file. By default, this property holds entries for middleware that are already included in Laravel. To add your custom middleware, you need to append them to the list and add a key of your choice.
protected $routeMiddleware = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
'userAuth' => \Illuminate\Routing\Middleware\UserAuthRequests::class,
];
Middleware with parameters:
Middleware can also take parameters. For example, you may have a middleware that checks the role of a user before allowing them to access certain parts of your application. To pass parameters to a middleware, you can use the following syntax:
public function handle($request, Closure $next, $profile)
{
if (! $request->user()->hasProfile($profile)) {
// Next page
}
return $next($request);
}
In addition to handling requests, middleware can also be used to perform tasks after a response has been sent to the browser. This is done using the terminate method. When a termination method is used in your project’s middleware, it gets called automatically after the browser response has been sent.
public function terminate($request, $response)
{
// tasks assigned within terminate method
}
Summary:
In conclusion, middleware is an essential component of Laravel that allows you to filter and modify HTTP requests and responses. It can be used for tasks such as authentication, authorization, and data validation, and can be created, registered, and used in various ways. Understanding and utilizing middleware in your Laravel projects can help you build more robust and secure applications.