Skip to content

Instantly share code, notes, and snippets.

@zburgermeiszter
Last active August 29, 2015 14:07
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 zburgermeiszter/07cab9004d19ed2cd961 to your computer and use it in GitHub Desktop.
Save zburgermeiszter/07cab9004d19ed2cd961 to your computer and use it in GitHub Desktop.
List all routes implemented by current controller
<?php
/**
* Return route names and details provided by the controller.
* Source: http://stackoverflow.com/questions/15943780/symfony2-get-list-of-all-routes-of-a-controller/15950365#15950365
* @return null
*/
protected function getControllerRoutes() {
/**
* @var $router \Symfony\Component\Routing\Router
* @var $collection \Symfony\Component\Routing\RouteCollection
* @var $params \Symfony\Component\Routing\Route
*/
$schemeAndHttpHost = $this->getRequest()->getSchemeAndHttpHost();
$router = $this->container->get('router');
$collection = $router->getRouteCollection();
$allRoutes = $collection->all();
$routes = array();
foreach ($allRoutes as $route => $params)
{
$defaults = $params->getDefaults();
if (isset($defaults['_controller']))
{
$controllerAction = explode(':', $defaults['_controller']);
$controller = $controllerAction[0];
if (!isset($routes[$controller])) {
$routes[$controller] = array();
}
$routes[$controller][]= array($route => array(
'url' => $schemeAndHttpHost.$params->getPath(),
'methods' => $params->getMethods(),
'schemes' => $params->getSchemes(),
));
}
}
return isset($routes[get_class($this)]) ?
$routes[get_class($this)] : null ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment