【问题标题】:How to get all routes within a route group?如何获取路由组内的所有路由?
【发布时间】:2021-10-14 19:17:00
【问题描述】:

我需要打印一个主菜单,而不是存储链接/路线的数据库,我认为有一种方法可以获取命名组中的所有路线,但我发现的只是通过以下方式获取路线行动。

web.php

Route::group(['as' => 'main'], function () {
    Route::get('/', function () {
        return view('pages.start');
    })->name('Home');
    
    Route::get('/foobar', function () {
        return view('pages.foobar');
    })->name('Home');
    
    Route::get('/business', function () {
        return view('pages.business');
    })->name('Business');
});

我正在寻找类似的东西:

$routes = getRoutesByGroup('main');

我真的不敢相信当前的 Laravel 中不存在这样的功能,但我似乎找不到这个。我错过了什么?

【问题讨论】:

标签: laravel laravel-8


【解决方案1】:

也许这可以部分解决你的情况

function getRoutesByStarting($start = '')
{
    $list = \Route::getRoutes()->getRoutesByName();
    if (empty($start)) {
        return $list;
    }

    $routes = [];
    foreach ($list as $name => $route) {
        if (\Illuminate\Support\Str::startsWith($name, $start)) {
            $routes[$name] = $route;
        }
    }

    return $routes;
}

用法

getRoutesByStarting('main')

更通用的解决方案

function getRoutesByGroup(array $group = [])
{
    $list = \Route::getRoutes()->getRoutes();
    if (empty($group)) {
        return $list;
    }

    $routes = [];
    foreach ($list as $route) {
        $action = $route->getAction();
        foreach ($group as $key => $value) {
            if (empty($action[$key])) {
                continue;
            }
            $actionValues = Arr::wrap($action[$key]);
            $values = Arr::wrap($value);
            foreach ($values as $single) {
                foreach ($actionValues as $actionValue) {
                    if (Str::is($single, $actionValue)) {
                        $routes[] = $route;
                    } elseif($actionValue == $single) {
                        $routes[] = $route;
                    }
                }
            }
        }
    }
    
    return $routes;
}

用法

getRoutesByGroup(['middleware' => 'api']);
getRoutesByGroup(['middleware' => ['api']]);
getRoutesByGroup(['as' => 'api']);
getRoutesByGroup(['as' => 'api*']);
getRoutesByGroup(['as' => ['api*', 'main']]);

【讨论】:

  • 我明天试试,非常感谢!
  • 从中获得服务或特质有意义吗?这里的最佳做法是什么?
【解决方案2】:
$allRoutes = Route::getRoutes()->getRoutes(); // fetch all rotues as array
$name = 'main'; // specify your full route name

$grouped_routes = array_filter($allRoutes, function($route) use ($name) {
        $action = $route->getAction(); // getting route action
        if (isset($action['as'])) {
            if (is_array($action['as'])) {
                return in_array($name, $action['as']);
            } else {
                return $action['as'] == $name;
            }
        }
        return false;
    });
    // output of route objects in the 'main' group
    dd($grouped_routes);

【讨论】:

    猜你喜欢
    • 2016-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-13
    相关资源
    最近更新 更多