Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Matt Stauffer:
Middleware groups in Laravel 5.2
Dec 23, 2015 @ 15:28:31

In the next part of his series spotlighting features in the most recent release of the Laravel framework (5.2), Matt Stauffer continues with a look at middleware groups.

When you are creating a site of any significant size in Laravel, your routes file will often get pretty large. One of the first things I do in a new site is group my routes by logically distinct sections like "admin", "auth", "public". Usually each of these groups get their own set of middleware—admin, for example, gets auth. Maybe the API group gets a different auth middleware, and it might get an API-specific rate limiter or something else.

Laravel 5.2 has introduced something called middleware groups, which are essentially a shortcut to applying a larger group of middleware, using a single key.

He takes his example above and makes an "admin" middleware group that lets you combine individual middlewares into a single callable set. He shows how to update your HttpKernel.php file with the new "auth" group and nest the "web" and "auth" middleware inside. He talks briefly about how this handling has changed from 5.1, pointing out that things without the "web" middleware will not have access to cookies/sessions/CSRF handling. He then includes an example showing how to use this "admin" grouping in your routes, either directly on a route or through a route grouping.

tagged: laravel route middleware group tutorial

Link: https://mattstauffer.co/blog/middleware-groups-in-laravel-5-2


Trending Topics: