Reputation: 4596
I have global middleware and i need to get parameters from the routes defined in routes.php
. My $request->route()
is NULL
Upvotes: 4
Views: 1771
Reputation: 3646
Another approach is to make your middleware "global" manually.
Way #1
Put it into all $middlewareGroups
in app/Http/Kernel.php
:
protected $middlewareGroups = [
'web' => [
//...
YourGlobalMiddleware::class,
],
'api' => [
//...
YourGlobalMiddleware::class,
],
];
Way #2
Wrap all your routes into a group and assign your middleware to it:
Route::group(['middleware' => 'your_global_middleware'], function () {
//all your routes
});
Upvotes: 0
Reputation: 219938
You can't. The route has not been matched yet. Route parameters are only available in route middleware.
Think about it: it doesn't make much sense for a global middleware to have access to the route's parameters, since every route has different parameters.
You can however get the URI segments:
$id = $request->segment(2);
Pass it the number (1 based index) of the segment you want.
Upvotes: 7