[英]Get param in Routing - Slim Framework 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);
});
});
如果我理解正確,服務將無法訪問路由參數。 您可以訪問的只是容器本身,但是從$container->getRoutes()['<routename>']->getArguments()
獲取有關參數的信息可能很棘手(例如$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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.