Reputation: 1951
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
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
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;
}
The
bar:id
is used when you want to pass the string id
to the middleware.
Upvotes: 4