我想在我的GET路由中添加一个查询参数:
$app->get('/rooms', function (ServerRequestInterface $request, ResponseInterface $response, $args) {
try {
$room = new \Riecken\PBS\controller\RoomController();
$result = $room->getRoomsWithDetails();
$response = $response->withJson($result);
$response = $response->withStatus(200);
return $response;
}catch(Exception $e) {
$response->getBody()->write($e->getMessage());
return $response->withStatus($e->getCode());
}
});
我想做的是,我只想在键入“ expandAll”时执行此功能。
我用它搜索了一下,可以在Slim文档中找到一些东西: https://www.slimframework.com/docs/v3/objects/request.html
但是我不知道如何实现它。
所以在我的情况下:
如果“ expandAll”,我想执行上面看到的功能(getRoomWithDetails(),否则我想执行另一个功能。 有可能吗?
非常感谢您!
答案 0 :(得分:1)
您可以只将必需的查询参数传递给getRoomsWithDetails或仅添加一个if条件。
$app->get('/rooms', function (ServerRequestInterface $request, ResponseInterface $response, $args) {
try {
$expandAll = $request->getParam('expandAll');
$room = new \Riecken\PBS\controller\RoomController();
if ($expandAll) {
$result = $room->getRoomsWithDetails();
} else {
$result = $room->anotherMethod();
}
$response = $response->withJson($result);
$response = $response->withStatus(200);
return $response;
} catch(Exception $e) {
$response = $response->withJson(['error' => ['message' => $e->getMessage()]]);
return $response->withStatus(500);
}
});