如何在Symfony2中获取控制器的所有路由列表?

时间:2022-09-15 19:16:18

I have a controller which implements all routes/URL(s). I had the idea to offer a generic index over all help-pages.

我有一个控制器,它实现所有路由/ URL。我有想法在所有帮助页面上提供通用索引。

Is there a way to get all routes defined by a controller (from within a controller) in Symfony2?

有没有办法在Symfony2中获取控制器(来自控制器内)定义的所有路由?

4 个解决方案

#1


23  

You could get all of the routes, then create an array from that and then pass the routes for that controller to your twig.

您可以获取所有路由,然后从中创建一个数组,然后将该控制器的路由传递给您的树枝。

It's not a pretty way but it works.. for 2.1 anyways..

它不是一个漂亮的方式,但它的工作..对于2.1反正..

    /** @var $router \Symfony\Component\Routing\Router */
    $router = $this->container->get('router');
    /** @var $collection \Symfony\Component\Routing\RouteCollection */
    $collection = $router->getRouteCollection();
    $allRoutes = $collection->all();

    $routes = array();

    /** @var $params \Symfony\Component\Routing\Route */
    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][]= $route;
        }
    }

    $thisRoutes = isset($routes[get_class($this)]) ?
                                $routes[get_class($this)] : null ;

#2


107  

What you can do is use the cmd with (up to SF2.6)

您可以做的是使用cmd(最高SF2.6)

php app/console router:debug

With SF 2.7 the command is

使用SF 2.7命令是

php app/console debug:router

With SF 3.0 the command is

使用SF 3.0命令是

php bin/console debug:router

which shows you all routes.

它显示了所有路线。

If you define a prefix per controller (which I recommend) you could for example use

如果你为每个控制器定义一个前缀(我推荐),你可以使用

php app/console router:debug | grep "<prefixhere>"

to display all matching routes

显示所有匹配的路线

To display get all your routes in the controller, with basically the same output I'd use the following within a controller (it is the same approach used in the router:debug command in the symfony component)

要显示获取控制器中的所有路由,基本上具有相同的输出,我将在控制器中使用以下内容(这与路由器中使用的方法相同:symfony组件中的debug命令)

/**
 * @Route("/routes", name="routes")
 * @Method("GET")
 * @Template("routes.html.twig")
 *
 * @return array
 */
public function routeAction()
{
    /** @var Router $router */
    $router = $this->get('router');
    $routes = $router->getRouteCollection();

    foreach ($routes as $route) {
        $this->convertController($route);
    }

    return [
        'routes' => $routes
    ];
}


private function convertController(\Symfony\Component\Routing\Route $route)
{
    $nameParser = $this->get('controller_name_converter');
    if ($route->hasDefault('_controller')) {
        try {
            $route->setDefault('_controller', $nameParser->build($route->getDefault('_controller')));
        } catch (\InvalidArgumentException $e) {
        }
    }
}

routes.html.twig

routes.html.twig

<table>
{% for route in routes %}
    <tr>
        <td>{{ route.path }}</td>
        <td>{{ route.methods|length > 0 ? route.methods|join(', ') : 'ANY' }}</td>
        <td>{{ route.defaults._controller }}</td>
    </tr>
{% endfor %}
</table>

Output will be:

输出将是:

/_wdt/{token} ANY web_profiler.controller.profiler:toolbarAction etc.

/ _wdt / {token} ANY web_profiler.controller.profiler:toolbarAction等。

#3


17  

I was looking to do just that and after searching the code, I came up with this solution which works for a single controller (or any ressource actually). Works on Symfony 2.4 (I did not test with previous versions) :

我正在寻找这样做,在搜索代码之后,我想出了这个适用于单个控制器(或实际上任何资源)的解决方案。适用于Symfony 2.4(我没有使用以前的版本进行测试):

$routeCollection = $this->get('routing.loader')->load('\Path\To\Controller\Class');

foreach ($routeCollection->all() as $routeName => $route) {
   //do stuff with Route (Symfony\Component\Routing\Route)
}

#4


0  

In Symfony 4 i wanted to get all the routes including controller and actions in one list. In rails you can get this by default.

在Symfony 4中,我希望将所有路径(包括控制器和操作)放在一个列表中。在rails中,您可以默认使用此功能。

In Symfony you need to add the parameter show-controllers to the debug:router command.

在Symfony中,您需要将参数show-controllers添加到debug:router命令。

If somebody looking for the same feature it can be get with:

如果有人在寻找相同的功能,可以使用:

bin/console debug:router --show-controllers

this will produce a list like the following

这将生成如下列表

------------------------------------------------------------------------- -------------------------------------
Name                   Method    Scheme    Host     Path                    Controller
------------------------------------------------------------------------- -------------------------------------
app_some_good_name     ANY       ANY       ANY      /example/example        ExampleBundle:Example:getExample
------------------------------------------------------------------------- -------------------------------------

#1


23  

You could get all of the routes, then create an array from that and then pass the routes for that controller to your twig.

您可以获取所有路由,然后从中创建一个数组,然后将该控制器的路由传递给您的树枝。

It's not a pretty way but it works.. for 2.1 anyways..

它不是一个漂亮的方式,但它的工作..对于2.1反正..

    /** @var $router \Symfony\Component\Routing\Router */
    $router = $this->container->get('router');
    /** @var $collection \Symfony\Component\Routing\RouteCollection */
    $collection = $router->getRouteCollection();
    $allRoutes = $collection->all();

    $routes = array();

    /** @var $params \Symfony\Component\Routing\Route */
    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][]= $route;
        }
    }

    $thisRoutes = isset($routes[get_class($this)]) ?
                                $routes[get_class($this)] : null ;

#2


107  

What you can do is use the cmd with (up to SF2.6)

您可以做的是使用cmd(最高SF2.6)

php app/console router:debug

With SF 2.7 the command is

使用SF 2.7命令是

php app/console debug:router

With SF 3.0 the command is

使用SF 3.0命令是

php bin/console debug:router

which shows you all routes.

它显示了所有路线。

If you define a prefix per controller (which I recommend) you could for example use

如果你为每个控制器定义一个前缀(我推荐),你可以使用

php app/console router:debug | grep "<prefixhere>"

to display all matching routes

显示所有匹配的路线

To display get all your routes in the controller, with basically the same output I'd use the following within a controller (it is the same approach used in the router:debug command in the symfony component)

要显示获取控制器中的所有路由,基本上具有相同的输出,我将在控制器中使用以下内容(这与路由器中使用的方法相同:symfony组件中的debug命令)

/**
 * @Route("/routes", name="routes")
 * @Method("GET")
 * @Template("routes.html.twig")
 *
 * @return array
 */
public function routeAction()
{
    /** @var Router $router */
    $router = $this->get('router');
    $routes = $router->getRouteCollection();

    foreach ($routes as $route) {
        $this->convertController($route);
    }

    return [
        'routes' => $routes
    ];
}


private function convertController(\Symfony\Component\Routing\Route $route)
{
    $nameParser = $this->get('controller_name_converter');
    if ($route->hasDefault('_controller')) {
        try {
            $route->setDefault('_controller', $nameParser->build($route->getDefault('_controller')));
        } catch (\InvalidArgumentException $e) {
        }
    }
}

routes.html.twig

routes.html.twig

<table>
{% for route in routes %}
    <tr>
        <td>{{ route.path }}</td>
        <td>{{ route.methods|length > 0 ? route.methods|join(', ') : 'ANY' }}</td>
        <td>{{ route.defaults._controller }}</td>
    </tr>
{% endfor %}
</table>

Output will be:

输出将是:

/_wdt/{token} ANY web_profiler.controller.profiler:toolbarAction etc.

/ _wdt / {token} ANY web_profiler.controller.profiler:toolbarAction等。

#3


17  

I was looking to do just that and after searching the code, I came up with this solution which works for a single controller (or any ressource actually). Works on Symfony 2.4 (I did not test with previous versions) :

我正在寻找这样做,在搜索代码之后,我想出了这个适用于单个控制器(或实际上任何资源)的解决方案。适用于Symfony 2.4(我没有使用以前的版本进行测试):

$routeCollection = $this->get('routing.loader')->load('\Path\To\Controller\Class');

foreach ($routeCollection->all() as $routeName => $route) {
   //do stuff with Route (Symfony\Component\Routing\Route)
}

#4


0  

In Symfony 4 i wanted to get all the routes including controller and actions in one list. In rails you can get this by default.

在Symfony 4中,我希望将所有路径(包括控制器和操作)放在一个列表中。在rails中,您可以默认使用此功能。

In Symfony you need to add the parameter show-controllers to the debug:router command.

在Symfony中,您需要将参数show-controllers添加到debug:router命令。

If somebody looking for the same feature it can be get with:

如果有人在寻找相同的功能,可以使用:

bin/console debug:router --show-controllers

this will produce a list like the following

这将生成如下列表

------------------------------------------------------------------------- -------------------------------------
Name                   Method    Scheme    Host     Path                    Controller
------------------------------------------------------------------------- -------------------------------------
app_some_good_name     ANY       ANY       ANY      /example/example        ExampleBundle:Example:getExample
------------------------------------------------------------------------- -------------------------------------