Nested group parameters? Am I doing it wrong?

Im developing a RESTFUL api with Slim and Im trying to work out how to get nested group parameters working.

Lets say I have a route like so:

/users/james/images/218/comments

Im going to want to group these up like so:

users(uid)->images(id)->comments(*offset)…

How would I go about doing this?

Thanks.

I"m sorry, but I don’t understand what you mean by grouping them up.

The route would look something like

$app->get('/users/{user_id}/images/{image_id}/comments', function($request, $response, $args) {
    $userId = $args['user_id'];
    $imageId = $args['image_id'];

    // do something here and return $response
});

I just thought it would probably be best to make use of Slims grouping function like so:

$app->group("/users/:user_id", function() use($app){
    $app->group("/images/:img_id", function() use($app){
        $app->get("/comments, function() use($app){
            // How do I get the parent groups paremeters from here? I know I can get my first parent, but what about my second parent?
        });
    });
});

In Slim 3, this just works:

$app->group("/users/{user_id}", function() {
    $this->group("/images/{img_id}", function() {
        $this->get("/comments", function($request, $response, $args) {
            $userId = $args['user_id'];
            $imageId = $args['img_id'];
        });
    });
});

No idea about Slim 2, sorry.

1 Like

This is probably due to the fact Im using Slim 2, looks like Il be upgrading, then.

Cheers.

They should be continuously passed down.

$app->group("/users/:user_id", function() use($app){
    $app->group("/images/:img_id", function() use($app){
        $app->get("/comments", function($user_id, $img_id) use($app){
            var_dump($user_id, $img_id);
        });
    });
});