Middleware
- What is middleware
- Types of middleware
What is middleware?

- located at app/Http/Middleware directory
- Middleware acts as a bridge between a request and a response. It is a type of filtering mechanism.
- Laravel includes a middleware that verifies whether the user of the application is authenticated or not. If the user is authenticated, it redirects to the home page otherwise, if not, it redirects to the login page.
- Middleware can be created by executing the following command
php artisan make:middleware <middleware-name>
Types of middleware
- Global Middleware : will run on every HTTP request of the application
- Route Middleware : will be assigned to a specific route
- middleware can be registered at app/Http/Kernel.php
- To register the global middleware, list the class at the end of $middleware property.
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
];
To register the route specific middleware, add the key and value to $routeMiddleware property.
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
];