Laravel 5 - Getting URL Parameters in Resource Middleware

Let's say that I have a resource defined in my Routes as:

Route::resource('account', 'AccountController', ['only'=> ['index','update']]);

      

And then I have Middleware

it attached to the Controller

inside like:

public function __construct() {
    $this->middleware('BeforeAccount', ['only' => ['update']]);
}

      

Let's say I want to access the uri parameter that happens after the account (i.e. example.com/account/2

) in my staging environment - how do I go about capturing this variable?

+3


source to share


2 answers


You can use the following code for this:

public function handle($request, Closure $next)
{
    $account_id = $request->route()->parameter('accounts');

    //...
}

      



Since the method handle

receives an object Request

as the first argument. middleware

is only executed after the route has been matched, so the object Request

contains the current route and does not need to be re-matched using Route::getRoutes()->match($request)

.

+2


source


This way you don't need to provide a \ Request object:



Route::current()->parameter('parameter');

      

0


source







All Articles