Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 4 print all routes

I'm trying to set up an index page which would print out all the endpoints in the API (using Symfony 4).

In Symfony 2 you could get a router and via the container and then the collection of the routes. But it seems that you don't have access to the container right out of the box in Symfony 4.

Google search doesn't seem to yield precise result I'm looking for. Is there an alternative way to do so in Symfony 4 or something alike?

like image 629
RokDev Avatar asked May 30 '18 12:05

RokDev


2 Answers

So I put the pieces together:

Simplest way seems to be to inject Symfony\Component\Routing\RouterInterface and then use it as a Router. As I mentioned in the question, you can get the routes by using $router->getRouteCollection()->all() where $router is the injected dependency.

E.g.:

use Symfony\Component\Routing\RouterInterface;

public function someMethodInController(Request $request, RouterInterface $router)
{
    $routes = $router->getRouteCollection()->all();
    // ...
}
like image 123
RokDev Avatar answered Sep 26 '22 13:09

RokDev


Use in any controller, this will return you an array of avaiable route objects.

$router = $this->get('router');
$routes = $router->getRouteCollection()->all();
like image 23
Zeppe Avatar answered Sep 25 '22 13:09

Zeppe