Home > Article > PHP Framework > What is the use of laravel middleware?
In laravel, the role of middleware is to filter HTTP requests and perform different logical operations according to different requests; the middleware can intercept and process the request data and inspect the data, and perform logical processing to determine whether Allows entry to the next middleware.
#The operating environment of this article: Windows 10 system, Laravel version 6, Dell G3 computer.
Middleware, as the name suggests, intercepts and processes request data, verifies data, and determines whether to allow entry after logical processing between the request and the response. Next middleware; middleware is divided into prefix middleware and post-middleware; it can be used for authority authentication, logging, etc.
Simply put, the role of middleware in laravel is to filter HTTP requests and perform different logical operations based on different requests.
We can achieve the following functions through middleware:
Specify certain routes
Settings HTTP response header
Record request
Filter request parameters
Determine whether to enable site maintenance Mode
Do some necessary operations before and after the response
Custom middleware
Command line You can easily create a new middleware by executing the following simple command
php artisan make:middleware <MiddlewareName> //MiddlewareName 就是你要创建的中间件的名字
Execute the above command, Laravel will automatically create a middleware containing only the handle method in the app/Http/Middleware directory.
<?php namespace App\Http\Middleware; use Closure; class RedirectIfSuperAdmin { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { return $next($request); } }
When the middleware is called, the handle method will be executed. What needs to be noted here is that the handle method has two parameters by default $request and $next. $request is used to accept the application's request group, and $next passes the request to the application. These two parameters are essential for handle! Middleware also includes pre-middleware and post-middleware.
"Pre-middleware" as the name suggests handles some logic before forwarding the request to the application. After middleware, on the other hand, runs after the application has processed the request and generated the response.
Pre-middleware:
<?php namespace App\Http\Middleware; use Closure; class RedirectIfSuperAdmin { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { //你的逻辑就在这里 return $next($request); } }
Post-middleware:
<?php namespace App\Http\Middleware; use Closure; class RedirectIfSuperAdmin { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { $response = $next($request); //你的逻辑就在这里 例如 重定向到 `/` return $response; } }
[Related recommendations: laravel video tutorial】
The above is the detailed content of What is the use of laravel middleware?. For more information, please follow other related articles on the PHP Chinese website!