Home  >  Article  >  PHP Framework  >  The functions of three middlewares in Laravel

The functions of three middlewares in Laravel

藏色散人
藏色散人forward
2021-05-26 16:10:442139browse

The following tutorial column will introduce to you the functions of the three middlewares in Laravel. I hope it will be helpful to friends in need! Before, I simply thought that middleware was just adding middleware to middleware. Now I know that there are three types of middleware, namely: $middleware $middlewareGroup $routeMiddleware After consulting the information, I finally understood the functions and differences of these three types.

The first type,

Global middleware/$middleware:Every time we make a request, here is Every middleware will be executed.

The second type,

routing middleware/$routeMiddleware:The middleware defined in this attribute, Can only be referenced when defining routes. Assume this is the routing middleware we defined:

protected $routeMiddleware = [
 ...
 'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
];

If we want to use \Illuminate\Auth\Middleware\Authenticate::class, this middleware should be written like this:

Route::get('hello/laravel-china','XXController@index')->middleware('auth');

Call the middleware method when defining a route, and the parameter value is auth. In this way, when this route is accessed, the middleware will be executed.



The third type,

Middleware Groups/$middlewareGroups:Look at the routing middleware above , we can feel that adding routing middleware in this way is very troublesome. If we want to execute 100 middleware, we must add 100 when defining the route. But with the middleware group, you don’t have to be so troublesome! Let's take a look at how the middleware group is defined.

protected $middlewareGroups = [
 'web' => [
 \App\Http\Middleware\EncryptCookies::class,
 ...
 ],
 'api' => [
 ...
 ],
];
The above example is to define the format of the middleware group. For example, the key value of web corresponds to an array, which is composed of multiple middlewares.

When you need to declare a route, just call it as follows.

Route::group(['middleware' => 'web'],function($route){
 $route->get('hello/world',function(){});
 $route->get('hello/php',function(){});
 // 这样在访问这个这些路由的时候,就会执行中间件组 web 所对应的中间件!
});

**Summary:

$middleware/global middleware, when you need to do some processing on all requests, it is suitable to be defined in this within the property. (For example, counting the number of requests)
$middlewareGroups/Middleware Groups, for example, when our project has api requests and web requests, we need to separate the two types of request middleware. At this time, We need our middleware group.

$routeMiddleware/Routing middleware, for some individual requests, when we need to execute special middleware, it is suitable to define it in this attribute. **

Related recommendations:

The latest five Laravel video tutorials

The above is the detailed content of The functions of three middlewares in Laravel. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete