Last active
January 13, 2022 08:37
-
-
Save alcalyn/b434e75c8b1666e3959e to your computer and use it in GitHub Desktop.
Dump all Silex routes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
... ... | |
========= ======================================== | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In case the script "as is" does not work in your app, try adding the code of your
index.php
before callingdisplayRoutes($app)
.