Alex
Alex

Reputation: 10226

How to get all routes within a route group?

I need to print a main menu and instead of having a database where the links/ routes are stored I thought it there would be a way to get all routes that are in a named group, but all I find is getting routes by action.

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');
});

I was looking for something like:

$routes = getRoutesByGroup('main');

I cannot really believe that a function like that doesnt exist in current Laravel but I cant seem to find this. What am I missing?

Upvotes: 1

Views: 1589

Answers (2)

Davit Zeynalyan
Davit Zeynalyan

Reputation: 8618

Maybe this can solve partially in your case

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;
}

usage

getRoutesByStarting('main')

More general solution

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;
}

usage

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

Upvotes: 1

Yudiz Solutions
Yudiz Solutions

Reputation: 4459

$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);

Upvotes: 0

Related Questions