LARAVEL

Middleware groups in Laravel 5.2


This is a series of posts that showcase new features and aha moments in Laravel 5.2

1. Implicit Route Model Binding

2. Simplified Eloquent Global Scopes

3. Append Scheduled Tasks Output

4. Form Array Validation

5. Middleware Groups

6. Auth Scaffolding

7.  Api Rate Limiting

8. More helper functions

Laravel 5.2 has a new feature that allows you group several route middleware under a single name/key thus helping you assign several middleware to a route/group of routes at once.

Laravel 5.2 comes shipped with two major middleware groups named web and api.

Let’s take a look.

1. Open AppHttpKernel.php. You will see something like this:

2. Open routes.php, you will see something like this:

Every route you stuff inside this Route group will have all the middleware grouped under web have effect on it.

Also, for your API routes, you can also just apply the api middleware group like so:

By default, it applies a rate limit of 60 requests per minute to all the routes above.

You can apply other middleware to the api group by just adding it in the api array in Kernel.php.

It’s that simple.

Please, let me know if you have any questions or observations in the comments section below.

PROSPER OTEMUYIWA

About PROSPER OTEMUYIWA

Food Ninja, Code Slinger, Technical Trainer, Accidental Writer, Open Source Advocate and Developer Evangelist.