Нужно разработать свой сервис (API), который будет реализовывать базовый функционал доступа к данным используя стиль REST.Сделал на slim.
А дальше написано - "должен быть использован максимально возможный спектр HTTP-статусов."Что это значит?Раньше в rest не лазил вообще и без понятия как доделать.
Т.е это коды ответа http?
https://developer.mozilla.org/ru/docs/Web/HTTP/Status
Как с ними работать?Буду признателен хотя бы за ссылки или примеры.
Код:
$app->group('/api', function (RouteCollectorProxy $group) {
$group->get('/products[/]',function(Request $request, Response $response){
$sql = "SELECT * FROM products";
try {
$db = new DB();
$conn = $db->connect();
$stmt = $conn->query($sql);
$products = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
$response->getBody()->write(json_encode($products));
return $response
->withHeader('content-type', 'application/json')
->withStatus(200);
} catch (PDOException $e){
$error = array(
"message" => $e->getMessage()
);
$response->getBody()->write(json_encode($error));
return $response
->withHeader('content-type', 'application/json')
->withStatus(500);
}
});
$group->get('/products/{id}/',function(Request $request, Response $response,array $args){
$id = $args['id'];
$sql = "SELECT * FROM products WHERE id = $id";
try {
$db = new DB();
$conn = $db->connect();
$stmt = $conn->query($sql);
$product = $stmt->fetch(PDO::FETCH_OBJ);
$db = null;
$response->getBody()->write(json_encode($product));
return $response
->withHeader('content-type', 'application/json')
->withStatus(200);
} catch (PDOException $e){
$error = array(
"message" => $e->getMessage()
);
$response->getBody()->write(json_encode($error));
return $response
->withHeader('content-type', 'application/json')
->withStatus(500);
}
});
$group->post('/products[/]',function(Request $request, Response $response,array $args){
$parameters = json_decode($request->getBody(), TRUE);
$product_name = $parameters['product_name'];
$product_price = $parameters['product_price'];
$product_count = $parameters['product_count'];
$sql = "INSERT INTO products (product_name,product_price,product_count) VALUES(:product_name,:product_price,:product_count)";
try {
$db = new DB();
$conn = $db->connect();
$stmt = $conn->prepare($sql);
$stmt->bindParam(':product_name',$product_name);
$stmt->bindParam(':product_price',$product_price);
$stmt->bindParam(':product_count',$product_count);
$result = $stmt->execute();
$db = null;
$response->getBody()->write(json_encode($result));
return $response
->withHeader('content-type', 'application/json')
->withStatus(200);
} catch (PDOException $e){
$error = array(
"message" => $e->getMessage()
);
$response->getBody()->write(json_encode($error));
return $response
->withHeader('content-type', 'application/json')
->withStatus(500);
}
});
$group->put('/products/{id}/',function(Request $request, Response $response,array $args){
$parameters = json_decode($request->getBody(), TRUE);
$id = $parameters['id'];
$product_name = $parameters['product_name'];
$product_price = $parameters['product_price'];
$product_count = $parameters['product_count'];
$sql = "UPDATE products SET product_name =:product_name , product_price=:product_price,product_count=:product_count WHERE id = $id";
try {
$db = new DB();
$conn = $db->connect();
$stmt = $conn->prepare($sql);
$stmt->bindParam(':product_name',$product_name);
$stmt->bindParam(':product_price',$product_price);
$stmt->bindParam(':product_count',$product_count);
$result = $stmt->execute();
$db = null;
$response->getBody()->write(json_encode($result));
return $response
->withHeader('content-type', 'application/json')
->withStatus(200);
} catch (PDOException $e){
$error = array(
"message" => $e->getMessage()
);
$response->getBody()->write(json_encode($error));
return $response
->withHeader('content-type', 'application/json')
->withStatus(500);
}
});
$group->delete('/products/{id}/',function(Request $request, Response $response,array $args){
$id = $args['id'];
$sql = "DELETE FROM products WHERE id = $id";
try {
$db = new DB();
$conn = $db->connect();
$stmt = $conn->prepare($sql);
$result = $stmt->execute();
$db = null;
$response->getBody()->write(json_encode($result));
return $response
->withHeader('content-type', 'application/json')
->withStatus(200);
} catch (PDOException $e){
$error = array(
"message" => $e->getMessage()
);
$response->getBody()->write(json_encode($error));
return $response
->withHeader('content-type', 'application/json')
->withStatus(500);
}
});
// var_dump($_SERVER['REQUEST_METHOD']);
});