0

在这种情况下如何获得参数?

$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;
}

提前致谢。

更新

在我的情况下的解决方案:

$app->group('/id', function () {
    $this->get('/{id}', function($request, $response, $args) {
        $this['container'] = $args; //work with $args inside the container
        return $this->singleSelect($id);
    });
});
4

1 回答 1

0

如果我正确理解服务无法访问路由参数。您只能访问容器本身,但从中获取有关参数的信息可能会很棘手(例如路由器可以$container->getRoutes()['<routename>']->getArguments()在哪里<routename>拥有子路由等)

恕我直言,您的代码应如下所示:

$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();
于 2016-01-30T10:32:06.767 回答