"No Records Found" 时发送 StatusCode 400
Send StatusCode 400 when "No Records Found"
当MySql响应不成功时,如何在slim v2中将StatusCode设置为400?
$app->get('/gg/:user/:pass', 'gg');
function gg($user, $pass) {
$sql = "...";
try {
$db = getDB();
$stmt = $db->prepare($sql);
$stmt->bindParam("user", $user);
$stmt->bindParam("pass", $pass);
$stmt->execute();
$gs = $stmt->fetchAll(PDO::FETCH_OBJ);
if ($gs) {
....
} else {
http_response_code(400);
throw new PDOException('No records found.');
}
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
api returns 状态错误 200 但当未找到 mysql 数据时它应该更改为状态代码 400。
According to the slim2 manual the correct approach is:
$app->response->setStatus(400);
因为你在一个函数里面,所以你需要在里面得到 $app
,有几种方法:
在这种情况下我建议:http://docs.slimframework.com/configuration/names-and-scopes/
$app = Slim::getInstance();
当MySql响应不成功时,如何在slim v2中将StatusCode设置为400?
$app->get('/gg/:user/:pass', 'gg');
function gg($user, $pass) {
$sql = "...";
try {
$db = getDB();
$stmt = $db->prepare($sql);
$stmt->bindParam("user", $user);
$stmt->bindParam("pass", $pass);
$stmt->execute();
$gs = $stmt->fetchAll(PDO::FETCH_OBJ);
if ($gs) {
....
} else {
http_response_code(400);
throw new PDOException('No records found.');
}
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
api returns 状态错误 200 但当未找到 mysql 数据时它应该更改为状态代码 400。
According to the slim2 manual the correct approach is:
$app->response->setStatus(400);
因为你在一个函数里面,所以你需要在里面得到 $app
,有几种方法:
在这种情况下我建议:http://docs.slimframework.com/configuration/names-and-scopes/
$app = Slim::getInstance();