列出php slim 3中所有包含组的路由

时间:2018-08-15 22:54:25

标签: php slim-3

我需要在Slim 3中将所有路由分组为带有子路由的数组。

这是我的代码:

$routes = $app->getContainer()->router->getRoutes();
$routes_list = [];
foreach ($routes as $index => $route) {
   $routes_list[$index]['name'] = $route->getName();
   $routes_list[$index]['pattern'] = $route->getPattern();
   $routes_list[$index]['callable'] = $route->getCallable();
   $routes_list[$index]['methods'] = $route->getMethods();
}

1 个答案:

答案 0 :(得分:1)

我有解决办法

我的代码

    $routes = $app->getContainer()->router->getRoutes();

    foreach ($routes as $index => $route) {
        $group = explode('.', $route->getName());
        if (is_array($group) and count($group) > 1) {
            $sub_group = explode('.', $route->getName());
            if (is_array($group) and count($group) > 2) {
                $routes_list[$group[0]][$group[1]][$index]['name'] = $route->getName();
                $routes_list[$group[0]][$group[1]][$index]['pattern'] = $route->getPattern();
                $routes_list[$group[0]][$group[1]][$index]['methods'] = $route->getMethods();
            } else {
                $routes_list[$group[0]][$index]['name'] = $route->getName();
                $routes_list[$group[0]][$index]['pattern'] = $route->getPattern();
                $routes_list[$group[0]][$index]['methods'] = $route->getMethods();
            }
        } else {
            $routes_list['routes'][$index]['name'] = $route->getName();
            $routes_list['routes'][$index]['pattern'] = $route->getPattern();
            $routes_list['routes'][$index]['methods'] = $route->getMethods();
        }
    }
  

它以这样的数组返回带有子溃烂的溃败组