Get Current Route Group Name

Hello,

I want to access the current route’s group name. For example, I have routes like this:

$app->group('/admin', function () use ($container) {
    $this->get('/books', BookController::class . ':index')->setName('admin.books.index');
    $this->get('/movies', MovieController::class . ':index')->setName('admin.movies.index');
})->add(new ExampleMiddleware($container));

In this example, I want to get current route’s group which is /admin in ExampleMiddleware.

How can I do this?

Retrieving Current Route

$route = $request->getAttribute('route');

    if (empty($route)) {
        throw new NotFoundException($request, $response);
    }

    $name = $route->getName();
    $groups = $route->getGroups();
    $methods = $route->getMethods();
    $arguments = $route->getArguments();

Thanks, @Mikalay. I tried this. But it didn’t help.

from the documentation @Mikalay linked too you might need to update your settings if you need this information with-in a middleware

$app = new App([
    'settings' => [
        // Only set this if you need access to route within middleware
        'determineRouteBeforeAppMiddleware' => true
    ]
]);

Then something like this should echo out /admin

    $route = $request->getAttribute('route');

    if (empty($route)) {
        throw new NotFoundException($request, $response);
    }

    //alter this last section to suit...
    $breadcrumbs = [];
    $groups = $route->getGroups();
    foreach ($groups as $group) {
      $breadcrumbs[]  = $group->getPattern();
    }
    echo join('', $breadcrumbs);
   die();

I know this is an old topic but google brought me here.