How to retrieve all routes as array I have a situation where I need to fetch all routes with name and url properties, but don't know how to do it. Does anybody know how to fetch or how to get route url using name in javascript?
use Illuminate\Routing\Router;
$router=new Router;
$routes=$router->getRoutes(); // it will return all routes as an object
So may be this trick will work to convert an object into the array but not sure.
$routes=(array) $router->getRoutes();
@veve286 Not working, this error is thrown
Type error: Argument 1 passed to Illuminate\Routing\Router::__construct() must implement interface Illuminate\Contracts\Events\Dispatcher, none given
app('router')->getRoutes();
so how about this ?
Route::getRoutes();
try this to parse
$object=Route::getRoutes();
$array=(array) $object;
$collection = Route::getRoutes();
$routes = [];
foreach($collection as $route) {
$routes[] = $route->getPath();
}
dd($routes);
Little late, but maybe helps someone.
function list_routes() {
return collect(Route::getRoutes())
->map(fn ($route) => $route->uri())
->filter()
->unique()
->values()
->all();
}
collect(Route::getRoutes())->mapWithKeys(function ($route) {
return [
$route->getName() => $route->uri(),
];
})
Please sign in or create an account to participate in this conversation.