Lumen route:
-------- --------------------------- ------------ ---------------------------------------------------- --------- --------------------------
| Verb | Path | NamedRoute | Controller | Action | Middleware |
-------- --------------------------- ------------ ---------------------------------------------------- --------- --------------------------
| GET | / | | None | Closure | |
| POST | /login | | App\Http\Controllers\Auth\AuthController | login | |
| GET | /setting/menu | | App\Http\Controllers\Setting\MenuController | index | auth, auth.authorization |
| POST | /setting/menu | | App\Http\Controllers\Setting\MenuController | store | auth, auth.authorization |
| GET | /setting/menu/{id} | | App\Http\Controllers\Setting\MenuController | show | auth, auth.authorization |
| PUT | /setting/menu/{id} | | App\Http\Controllers\Setting\MenuController | update | auth, auth.authorization |
| DELETE | /setting/menu/{id} | | App\Http\Controllers\Setting\MenuController | destroy | auth, auth.authorization |
-------- --------------------------- ------------ ---------------------------------------------------- --------- --------------------------
$request->route()
only return route name, How can I get the route path like /setting/menu/{id}
?
I used Lumen 9.x
CodePudding user response:
You should use
$request->path()
- Laravel docs: https://laravel.com/docs/9.x/requests#retrieving-the-request-path
- Lumen docs: https://lumen.laravel.com/docs/9.x/requests
CodePudding user response:
you better name your route to make it easier for you, https://lumen.laravel.com/docs/9.x/routing#named-routes.
like this
$router->get('setting/menu/{id}', ['as' => 'setting.menu.show', 'uses' => 'App\Http\Controllers\Setting\MenuController@show']);
$router->put('setting/menu/{id}', ['as' => 'setting.menu.update', 'uses' => 'App\Http\Controllers\Setting\MenuController@update']);
and then you can call
$path = '';
$routes = app()->router->getRoutes();
$name = $request->route()[1]['as'];
foreach ($routes as $key1 => $value1) {
foreach ($value1 as $key2 => $value2) {
if ( $key2 == 'action' ) {
foreach ($value1[ $key2 ] as $key3 => $value3) {
if ( $key3 == 'as' ) {
if ( $value2[ $key3 ] == $name ) {
$path = $key1;
break;
}
}
}
}
}
}
dd($path); // result GET/setting/menu/{id}, you can remove get from string.