苗条的查询参数
Slim query parameter
我想在我的 GET Route 中添加一个查询参数,就是这个:
$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()),否则我想执行另一个函数。
这可能吗?
非常感谢!
您可以只将所需的查询参数传递给 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);
}
});
我想在我的 GET Route 中添加一个查询参数,就是这个:
$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()),否则我想执行另一个函数。 这可能吗?
非常感谢!
您可以只将所需的查询参数传递给 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);
}
});