Logo Questions Linux Laravel Mysql Ubuntu Git Menu

How to get route parameters in Laravel Global Middleware?

I have global middleware and i need to get parameters from the routes defined in routes.php. My $request->route() is NULL

like image 297
Kin Avatar asked Mar 15 '23 09:03


1 Answers

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.

like image 182
Joseph Silber Avatar answered Mar 25 '23 02:03

Joseph Silber