Skip to content

Instantly share code, notes, and snippets.

@eukras
Created September 18, 2017 04:50
Show Gist options
  • Save eukras/5c4966036782c5afeb544fffcb9a876e to your computer and use it in GitHub Desktop.
Save eukras/5c4966036782c5afeb544fffcb9a876e to your computer and use it in GitHub Desktop.
Silex App: List routes in console.
<?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