Putting Global Middleware in your Laravel routes.php or Kernel.php?

I’ve been using Laravel Middleware for checking a certain user has been authenticated with the Uni’s SSO.

As a rule, anyone darkening our servers HAS to be a Warwick University Student and so, should be Auth’d by Shibboleth to the Uni’s Single Sign On.

The Laravel docs suggest that for “Global Middleware” you should put it in the app/Http/Kernel.php class and it will run on everything,

If you want a middleware to be run during every HTTP request to your application, simply list the middleware class in the $middleware property of your app/Http/Kernel.php class.

http://laravel.com/docs/5.0/middleware#registering-middleware

However, whilst that’s fine there is a good argument for putting it in the routes.php file, so it’s clear to any non-laravellers what Middleware is protecting the routes.

Route::group(['middleware' => ['IsUniSSOd']], function()

To be honest either’s fine. I started using the Route::group protection initially, however, it indents everything and starts to make it look a bit like a horrific massive if statement from days of old.

So now we’re putting it in our Kernel.php file to smarten it up as everyone gets more used to Laravel.

protected $middleware = [
...
'AppHttpMiddlewareIsUniSSOdMiddleware'
]

Author: Dan Course

Dan Course, E-Learning Development at Warwick University (WMG). Agile Scrum trained and full-stack development. Interested in E-learning, Democracy, Politics and Tech. All thoughts are mine and not of my employer.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s