Skip to content

Instantly share code, notes, and snippets.

@hubgit
Created August 17, 2012 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hubgit/3380250 to your computer and use it in GitHub Desktop.
Save hubgit/3380250 to your computer and use it in GitHub Desktop.
Symfony2 command to output routes as a table, including the defaut controller
<?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']));
}
}
}
@adatta02
Copy link

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

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