我目前正在學習如何使用適用於PHP的Slim Framework v3構建自己的REST API。我發現了幾個教程,並且能夠構建多條路由來將GET和POST請求發送到我的MySQL數據庫。Slim Framework v3 - REST API - 刪除
接下來是我的一個刪除請求。我發現了一個教程,呈現如下的一段代碼:
$app->delete('/todo/[{id}]', function ($request, $response, $args) {
$sth = $this->db->prepare("DELETE FROM tasks WHERE id=:id");
$sth->bindParam("id", $args['id']);
$sth->execute();
$todos = $sth->fetchAll();
return $this->response->withJson($todos);
});
在此之前我做了我刪除的路線(是,這些術語的正確使用?)幾乎是相同的,只是我沒有任何回報。這段代碼讓我想知道:刪除請求返回什麼是「常見」?從我的理解這個請求是假設返回表中的所有其他條目「任務」 - 這是一個好的做法?一般來說,如果刪除請求返回某些內容,是否是「良好」的做法 - 如果是:什麼?我正在嘗試適應最常見的做法。
在旁註:我試圖使用像這樣的引用請求,並能像往常一樣刪除一些東西。但是,它根本不返回任何東西。
誤差如下:
[...]
<h1>Slim Application Error</h1>
<p>The application could not run because of the following error:</p>
<h2>Details</h2>
<div>
<strong>Type:</strong> PDOException
</div>
<div>
<strong>Code:</strong> HY000
</div>
<div>
<strong>Message:</strong> SQLSTATE[HY000]: General error
</div>
<div>
<strong>File:</strong> /www/htdocs/src/routes.php
</div>
<div>
<strong>Line:</strong> 68
</div>
[...]
68行是在我的情況 「$待辦事項= $ sth->使用fetchall();」。
要添加關於「良好實踐」的另一個問題:我想檢查通過刪除請求給出的ID是否存在 - 是否將包含在路由中(檢查它是否存在) ?還是有另一種做這種檢查的做法?
非常感謝。