Larravel 5.2 New Feature Series -- Definition and Use of Middleware Group


No matter how large the Larravel application volume you create, route files routes.php Will become larger and larger. For me, the first thing to create a new application is to split and group the routing files according to the business logic, such as "admin", "auth", "public", etc. Usually, each part of the grouping has its corresponding middleware settings, for example, admin One will be used auth Middleware, API grouping may be different auth Middleware, and there will be specified middleware that limits access frequency.

Laravel 5.2 introduces the concept of middleware group, which is a shortcut to use multiple middleware for routing rules. Only one middleware group key needs to be defined.

Note: Even if you don't want to use the middleware group, you can continue to look down, because this is a major change in the Larravel global middleware stack.
So remember the admin example mentioned above? Now we can create an "admin" middleware group for it. Let's talk about how to create and use the middleware group in detail.

1. Define middleware group

You can app\Http\Kernel.php Middleware group is defined in. There are new array attributes in this class $middlewareGroups , the key of the array is the name of the middleware group, and the value is the corresponding middleware.

Larravel provides us with web and api Middleware group:

 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', ], ];

As you can see, $middlewareGroups The key of can be the middleware class name or the alias of the specified routing middleware, such as throttle or auth , let's create admin Middleware group:

 protected $middlewareGroups = [ 'web' => [...], 'api' => [...], 'admin' => [ 'web', 'auth', ] ];

We have defined admin Middleware group use web (another middleware group) and auth (alias of routing middleware), it is so simple!

Differences from Laravel 5.1

You may have noticed that, web The middleware in is the middleware applied to each route in Larravel 5.1 and earlier versions, which is a major ideological improvement: routes without web middleware groups will not have cookies, sessions or CSRF functions. For example, only allocate api This is the routing of the middleware group.

2. Using middleware groups

We have created a middleware group. How do we use it next?

If you've seen Laravel 5.2 routes.php It will be clear:

 Route::get('/', function () { return view('welcome'); }); Route::group(['middleware' => ['web']], function () { // });

As you can see, you can use middleware groups just like routing middleware: they can be set as specified middleware or middleware array, so we can use middleware groups like this admin

 Route::group(['middleware' => 'admin'], function () { Route::get('dashboard', function () { return view('dashboard'); }); });

It's that simple!


give the thumbs-up Cancel Like Collection Cancel Collection

<<Previous: Larave 5.3 will simplify the sub directory structure under the app directory

>>Next: Larravel 5.2 new feature series - the use of access frequency limiting middleware throttle