Route Nesting syntax in Slim4

Hi - trying to nest routes in Slim4. But i am getting errors and i cant find anything specific in the documentation around syntax for this. In my route config this works:

    $app->group('/user', function (RouteCollectorProxy $group) {
        $group->get('/accommodation/{accommodationid:\d+}/quote', QuoteHandler::class);
        $group->get('/accommodation/{accommodationid:\d+}/basenightlyrates/{id:\d+}', GetBaseNightlyRatesHandler::class);
        $group->delete('/accommodation/{accommodationid:\d+}/basenightlyrates/{id:\d+}', DeleteBaseNightlyRateHandler::class);
        $group->post('/accommodation/{accommodationid:\d+}/basenightlyrates', AddBaseNightlyRateHandler::class);
        $group->put('/accommodation/{accommodationid:\d+}/basenightlyrates/{id:\d+}', UpdateBaseNightlyRateHandler::class);
    })->add(new JsonHeader())->add(UserAuthentication::class);

This doesnt work:

    $app->group('/user', function (RouteCollectorProxy $group) {
        $app->group('/accommodation/{accommodationid:\d+}', function (RouteCollectorProxy $group) {
            $group->get('/quote', QuoteHandler::class);
            $group->get('/basenightlyrates/{id:\d+}', GetBaseNightlyRatesHandler::class);
            $group->delete('/basenightlyrates/{id:\d+}', DeleteBaseNightlyRateHandler::class);
            $group->post('/basenightlyrates', AddBaseNightlyRateHandler::class);
            $group->put('/basenightlyrates/{id:\d+}', UpdateBaseNightlyRateHandler::class);
        });
    })->add(new JsonHeader())->add(UserAuthentication::class);

I get following error on the nested $app->group statement…“Undefined variable: app”

Hello!

You can try using $group->group().

For example:

$app->group('/api/v1', function (RouteCollectorProxy $group) {
    $group->group('/product', function (RouteCollectorProxy $group) {
        $group->get('', Product\GetAll::class);
        $group->post('', Product\Create::class);
        ...
    });
});

or try to add an use ($app) just after function () in lines of $app->group.

For example:

$app->group('/api/v1', function () use ($app) {
    $app->group('/product', function () use ($app) {
        $app->get('', Product\GetAll::class);
        $app->post('', Product\Create::class);
	...
    });
});

(I don’t tested yet, just are ideas, in case of the error: Undefined variable: app.)