Created
September 18, 2017 04:50
-
-
Save eukras/5c4966036782c5afeb544fffcb9a876e to your computer and use it in GitHub Desktop.
Silex App: List routes in console.
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 '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([ | |
'methods', | |
'path', | |
'pattern' | |
]); | |
foreach ($app['routes'] as $route) { | |
$table->addRow([ | |
implode('|', $route->getMethods()), | |
$route->getPath(), | |
$route->getPattern(), | |
]); | |
} | |
$table->render(); | |
} | |
// Use it: | |
$silexApp = require 'app/app.php'; | |
// 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