Introduction to Middleware
Explore the role of middleware in Laravel to filter HTTP requests and responses. Understand how to create, register, and assign middleware to routes or controllers to manage authentication and authorization. Gain practical skills in controlling access and handling session logic for secure Laravel applications.
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.
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
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);}}