Как выводить HTTP статусы в SLIM
есть такой код на SLIM, необходимо выводить HTTP статусы при ошибках. Не очень в этом разбираюсь. Направьте молодой ум.
$app->post('/products', function (ServerRequestInterface $request, ResponseInterface $response, $next) use ($app) {
global $mysql;
$postJson = $request->getParsedBody();
echo "$postJson[du]";
$query = "INSERT INTO `tete`(`du`, `name`, `price`) VALUES ('$postJson[du]','$postJson[name]','$postJson[price]')";
if ($result = mysqli_query($mysql, $query)) {
echo "Все круто";
} else {
echo mysqli_error($mysql);
}
echo $response;
});
Ответы (1 шт):
Автор решения: Ivan Dudarev
→ Ссылка
Тут не через echo нужно делать. Нужно примерно так:
$app->post('/products', function (ServerRequestInterface $request, ResponseInterface $response, $next) use ($app) {
global $mysql; // Это очень плохо, ищите про внедрение зависимостей
$postJson = $request->getParsedBody();
$body = $response->getBody();
$body->write($postJson["du"]);
$query = "INSERT INTO `tete`(`du`, `name`, `price`) VALUES ('$postJson[du]','$postJson[name]','$postJson[price]')";
if ($result = mysqli_query($mysql, $query)) {
$body->write("Всё круто");
return $response;
} else {
$body->write(mysqli_error($mysql));
return $response->withStatusCode(500);
}
return $response;
});