Skip to content

Instantly share code, notes, and snippets.

@alcalyn
Last active January 13, 2022 08:37
Show Gist options
  • Select an option

  • Save alcalyn/b434e75c8b1666e3959e to your computer and use it in GitHub Desktop.

Select an option

Save alcalyn/b434e75c8b1666e3959e to your computer and use it in GitHub Desktop.
Dump all Silex routes
<?php
require_once __DIR__.'/../vendor/autoload.php';
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Silex\Application;
/**
* Display all routes registered in a Silex application.
* Important: the Silex application must have flushed its controllers before.
*
* @param Application $app
* @param OutputInterface $output
*/
function displayRoutes(Application $app, OutputInterface $output = null) {
if (null === $output) {
$output = new ConsoleOutput();
}
$table = new Table($output);
$table->setStyle('borderless');
$table->setHeaders(array(
'methods',
'path'
));
foreach ($app['routes'] as $route) {
$table->addRow(array(
implode('|', $route->getMethods()),
$route->getPath(),
));
}
$table->render();
}
// Use it:
$silexApp = new Silex\Application(); // Your app
// Dont forget:
$silexApp->flush();
// Display routes in console:
displayRoutes($silexApp);
/* Result:
$> php list-routes.php
========= ========================================
methods path
========= ========================================
GET /api/kittens/{id}
POST /api/kittens
GET /api/users
... ...
========= ========================================
*/
@nrctkno
Copy link
Copy Markdown

nrctkno commented Mar 2, 2021

In case the script "as is" does not work in your app, try adding the code of your index.php before calling displayRoutes($app).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment