Introduction to Middleware
Learn about middleware and its implementation in Laravel.
We'll cover the following...
Introduction
Middleware filters logic between the request and response of an application. It acts as a bridge that validates server-side and client-side HTTP requests.
How middleware works
Configuration of middleware
Laravel frameworks store middleware in the app\http\middleware
directory of the project. New middleware can be created using the following Artisan
command:
php artisan make:middleware exampleMiddleware
Middleware creation in Laravel
The command above will create a new file with a .php
extension in the app\http\middleware
directory. After the successful execution of the command, the following file will be created:
<?phpnamespace App\Http\Middleware;use Closure;use Illuminate\Http\Request;class exampleMiddleware{/*** Handle an incoming request.** @param \Illuminate\Http\Request $request* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse*/public function handle(Request $request, Closure $next){return $next($request);}}
The basic structure of middleware