如何使用Slim Framework 2的自定义路由器?

Trying to get a list of all routes loaded in Slim Framework. Need to know how to use a custom Router class which adds a method to get the protected $routes property.

It seems you can override Slim\Slim constructor and set the router that it will use, or you you can name all you routes and then you access the collection with the Slim\Router::getNamedRoutes() method.

<?php
$application->get(
    '/api',
    function () use ($application) {
        $routes = [];
        foreach ($application->router()->getNamedRoutes() as $route) {
            $routes[] = $route->getPattern();
        } 
        $application->response->headers->set('Content-Type', 'application/json');
        echo json_encode([
            'total' => count($routes),
            'routes' => $routes
        ]);
    }
)->name('api');

Which will response with.

{
    total: 1,
    routes: [
        "/api"
    ]
}