Symfony2:获取控制器的所有路由的列表 [英] Symfony2: get list of all routes of a controller

查看:352
本文介绍了Symfony2:获取控制器的所有路由的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制器,实现所有的路线/网址,用于显示帮助页面。
路由由注释定义。
我想在所有帮助页面上提供一个通用索引。有没有办法得到控制器定义的所有路由(从控制器中)?

I have a controller which implements all routes/urls that are used for showing the help-pages. The routes are defined by annotiations. I had the idea to offer a generic index over all help-pages. Is there a way to get all routes defined by a controller (from within a controller)?

推荐答案

路由,然后从那里创建一个数组,然后将该控制器的路由传递到你的树枝。

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

这不是一个漂亮的方式,但它的工作原理2.1 forways anyways ..

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

    /** @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 ;

这篇关于Symfony2:获取控制器的所有路由的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆