Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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']));
}
}
}

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