0

How can I get the param in this case?

$this->get('/{id}', function($request, $response, $args) {
  return $response->withJson($this->get('singleSelect'));
});

$this->appContainer['singleSelect'] = function ($id) {
  return $this->singleSelect($id);
};

public function singleSelect($id) {
  return $id;
}

Thanks in advance.

UPDATE

Solution in my case:

$app->group('/id', function () {
    $this->get('/{id}', function($request, $response, $args) {
        $this['container'] = $args; //work with $args inside the container
        return $this->singleSelect($id);
    });
});
alexw
  • 7,044
  • 6
  • 46
  • 81

1 Answers1

0

If I right understand services have not access to routes parameters. All you can access is container itself but it could be tricky to get information about arguments from it (something like $container->getRoutes()['<routename>']->getArguments() where <routename> router could have subroutes, etc.)

IMHO your code should look like:

$container = new \Slim\Container;
$app = new \Slim\App($container);

class Example {
    public function singleSelect($id) {
        return $id;
    }
}

$container['example'] = function () {
    return new Example();
};

$app->group('/id', function () {
    $this->get('/{id}', function($request, $response, $args) {
        return $response->withJson($this->example->singleSelect($args['id']));
    });
});

$app->run();
CrazyCrow
  • 3,597
  • 1
  • 22
  • 36