Nesting $app->group unable to access instance of $app

I am trying to implement versioning using nested group method but the inner $app instance calling
$app->group(’/mesages’) is giving me undefined variable $app for the inner instance
($app->group(’/messages’)).

$app->group('/v1', function() {
    $app->group('/messages', function() {
        $this->map(['GET'], '', function ($request, $response, $args) {
            $_message = new Message();

            $messages = $_message->all();

            $payload = [];
            foreach ($messages as $_msg) {
                $payload[$_msg->id] = $_msg->output();
            }

            return $response->withStatus(200)->withJson($payload);
        })->setName('get_messages');
    });
});

Inside the first group closure, $app == $this, you missed one instance of it :slight_smile:

$app->group('/v1', function() {
    $this->group('/messages', function() { // <--
        $this->map(['GET'], '', function ($request, $response, $args) {
            $_message = new Message();

            $messages = $_message->all();

            $payload = [];
            foreach ($messages as $_msg) {
                $payload[$_msg->id] = $_msg->output();
            }

            return $response->withStatus(200)->withJson($payload);
        })->setName('get_messages');
    });
});
1 Like
$app->group('/v1', function () use ($app) {
    $app->group('/messages', function() {
    // ...
1 Like

Perfect, thankyou, my bad on missing that one

1 Like