Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rohitkhatri's avatar

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?

0 likes
10 replies
veve286's avatar

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();

rohitkhatri's avatar

@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
veve286's avatar

try this to parse


$object=Route::getRoutes();
$array=(array) $object;

joedawson's avatar
$collection = Route::getRoutes();

$routes = [];

foreach($collection as $route) {
    $routes[] = $route->getPath();
}

dd($routes);
1 like
planetadeleste's avatar

Little late, but maybe helps someone.


function list_routes() {
    return collect(Route::getRoutes())
            ->map(fn ($route)  => $route->uri())
            ->filter()
            ->unique()
            ->values()
            ->all();
}

AddWebContribution's avatar
collect(Route::getRoutes())->mapWithKeys(function ($route) {
                return [
                    $route->getName() => $route->uri(),
                ];
            })

Please or to participate in this conversation.