Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get all registered routes #1456

Closed
Zyles opened this issue Aug 20, 2015 · 4 comments
Closed

Get all registered routes #1456

Zyles opened this issue Aug 20, 2015 · 4 comments

Comments

@Zyles
Copy link

Zyles commented Aug 20, 2015

Is it possible somehow to get all the registered routes, their HTTP methods, controllers and functions into an array?

I want to replicate "$ php artisan routes" from Laravel in a cli app so would be nice with this list.

@geggleto
Copy link
Member

Zyles, no.

In v3, the callables are only resolved once a route has been chosen to be dispatched.

However you can all of the Route objects through the router.

$app->getContainer()->get('router')->getRoutes();

@silentworks
Copy link
Member

You can do this using the code above.

@scones
Copy link

scones commented Jan 17, 2019

For the next poor soul landing here and getting the advice to use the deeply nested -thus unreadable- routes object:

        $routes = array_reduce($this->app->getContainer()->get('router')->getRoutes(), function ($target, $route) {
            $target[$route->getPattern()] = [
                'methods' => json_encode($route->getMethods()),
                'callable' => $route->getCallable(),
                'middlewares' => json_encode($route->getMiddleware()),
                'pattern' => $route->getPattern(),
            ];
            return $target;
        }, []);
        die(print_r($routes, true));

@drascom
Copy link

drascom commented May 24, 2020

better printout with path and methods

$routes = $app->getContainer()->router->getRoutes();
$list=array();
foreach ($routes as $route) {
    $list[]= $route->getPattern() .' '. json_encode($route->getMethods());
  }
print_r($list);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants