列出组在 php slim 3 中的所有路由
List all routes with group in 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();
}
我找到了解决方案
我的代码
$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();
}
}
it return routs group with child routs in array like this
我需要在 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();
}
我找到了解决方案
我的代码
$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();
}
}
it return routs group with child routs in array like this