在Routing - Slim Framework 3中获取参数

时间:2016-01-26 20:19:38

标签: php containers slim slim-3

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

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

1 个答案:

答案 0 :(得分: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();