Laravel Middleware makes it easy to filter requests for access to our web pages. This includes user authorization, cors to specify headers for outgoing requests, and so on.
To define a new middleware you can use the command:
make: Middleware agemiddleware
这条命令在app/Http/Middleware中创建AgeMiddleware类。
Let's say we filter all requests that are younger than 200:
<?phpnamespace App\http\middleware; UseClosure;classagemiddleware{/** * Run the request filter. * * @param \illuminate\http\request $request * @param \closure $next * @return Mixed*/ Public functionHandle$request, Closure$next) { if($request->input (' age ') <= 200) { returnRedirect (' home ')); } return $next($request); }}
You can pass the request down by calling the $next callback function.
Middleware filtering is not limited to the request arrival, when the request processing is complete, can also be filtered through the middleware processing:
<?phpnamespace App\http\middleware; UseClosure;classbeforemiddleware{ Public functionHandle$request, Closure$next) { //Perform Action return $next($request); }}<?phpnamespace App\http\middleware; UseClosure;classaftermiddleware{ Public functionHandle$request, Closure$next) { $response=$next($request); //Perform Action return $response; }}
If you need your middleware to be able to filter all Http requests, simply list them in the app/http/kernel.php $middleware.
If you need to assign middleware to a specific route, you first need to declare a simple key in the $routemiddleware in app/http/kernel.php:
protected $routeMiddleware = [ ' auth ' = \app\http\middleware\authenticate::class, ' Auth.basic ' = \illuminate\auth\middleware\authenticatewithbasicauth::class, ' guest ' and \app\ Http\middleware\redirectifauthenticated::class, ' throttle ' = \illuminate\routing\middleware\ Throttlerequests::class,];
Once declared, it can be used in the route, and the route may also specify multiple middleware:
function () { //}]); Route function () { //}]);
You can also specify by method:
function () { //})->middleware ([' First ', ' second ']);
We can also declare multiple middleware group in app/http/kernel.php as a middleware, such as our well-known web and API middleware:
protected $middlewareGroups= [ ' Web ' =[\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, ], ' API ' = [ ' throttle:60,1 ', ' Auth:api ', ],];
Middleware also accepts the arguments, just add the new parameters after the last variable $next. The parameters need to be used in route: separate middleware and arguments:
<?phpnamespace App\http\middleware; UseClosure;classrolemiddleware{/** * Run the request filter. * * @param \illuminate\http\request $request * @param \closure $next * @param string $role * @return Mixed*/ Public functionHandle$request, Closure$next,$role) { if(!$request->user ()->hasrole ($role)) { //Redirect ... } return $next($request); }}route::p ut (' Post/{id} ', [' middleware ' = ' role:editor ',function($id) { //}]);
Laravel Middleware Middleware Notes