brazorf
brazorf

Reputation: 1951

Laravel5 passing route args as middleware args

I would like the route argument to be passed as a middleware argument, something like this

Route::get('foo/{id}', ['middleware' => 'bar:{id}', function(){

});

How to do this?

Upvotes: 1

Views: 357

Answers (2)

user1669496
user1669496

Reputation: 33068

If you set the middleware up in the controller's constructor, it's possible to pass dynamic middleware variables.

public function __construct()
{
    $this->middleware('bar:'.request()->id);
}

Upvotes: 1

cre8
cre8

Reputation: 13562

You can get it from the request variable:

Route::get('foo/{id}', ['middleware' => 'bar', function(){

});

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

https://laracasts.com/discuss/channels/general-discussion/how-to-get-url-parameters-at-middleware?page=1

The

bar:id

is used when you want to pass the string id to the middleware.

Upvotes: 4

Related Questions