Created
August 17, 2012 16:10
-
-
Save hubgit/3380250 to your computer and use it in GitHub Desktop.
Symfony2 command to output routes as a table, including the defaut controller
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 | |
/* | |
* Output a summary of all routes as TSV | |
*/ | |
namespace Acme\DemoBundle\Command; | |
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | |
use Symfony\Component\Console\Input\InputArgument; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\OutputInterface; | |
use Symfony\Component\Console\Output\Output; | |
/** | |
* A console command for retrieving information about routes in a table | |
*/ | |
class RouterTableCommand extends ContainerAwareCommand | |
{ | |
/** | |
* @see Command | |
*/ | |
protected function configure() | |
{ | |
$this->setName('acme:router:table')->setDescription('Displays current routes for an application as TSV'); | |
} | |
/** | |
* @param \Symfony\Component\Console\Input\InputInterface $input input | |
* @param \Symfony\Component\Console\Output\OutputInterface $output output | |
* | |
* @return void | |
* | |
* @see Command | |
*/ | |
protected function execute(InputInterface $input, OutputInterface $output) | |
{ | |
$format = "%s\t%s\t%s\t%s"; | |
$output->writeln(sprintf($format, | |
'<comment>Name</comment>', | |
'<comment>Method</comment>', | |
'<comment>Pattern</comment>', | |
'<comment>Controller</comment>')); | |
foreach ($this->getContainer()->get('router')->getRouteCollection()->all() as $name => $route) { | |
/** @var $route \Symfony\Component\Routing\Route */ | |
$route = $route->compile(); | |
$requirements = $route->getRequirements(); | |
$method = isset($requirements['_method']) ? strtoupper(is_array($requirements['_method']) ? implode(', ', $requirements['_method']) : $requirements['_method']) : 'ANY'; | |
$defaults = $route->getDefaults(); | |
$output->writeln(sprintf($format, | |
$name, | |
$method, | |
$route->getPattern(), | |
$defaults['_controller'])); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
FYI, the "CompiledRoute" class no longer seems to have a getPattern() function:
https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Routing/CompiledRoute.php