Access route parameters in a Laravel 5 Middleware

In Laravel, when you specify your routes, you can place parameters in the URL. These parameters can also be automatically mapped to Model objects.

For example, you have a root where the path looks like this: /forum/{forumModel}/edit.

If you are writing a Middleware that should check something on the request and you need to access the parameter (forumModel in our case) in the URL, you can just do this:

<?php

class SampleMiddleware {

    /* ... */

    public function handle($request, Closure $next)
    {
        $forumModel = $request->forumModel;

        /* Do some stuff... */

        return $next($request);
    }

}

That’s it, the route parameters are accessible directly from the $request object.