Man*_*ann 4 php mysql api rest zend-framework2
我正在使用Apigility和Zend Framework 2构建REST API.在这个API中,我有一个代码连接的REST服务,当我尝试删除一个实体时,我遇到了一个奇怪的行为.我使用我的TableGateway对象的delete方法删除传递到"Resource"文件的delete方法的数据:
public function delete($id)
{
//GetTable returns a TableGateway instance
$this->getTable('order')->delete(array('id' => $id));
return array("status" => "deleted", "id" => $id);
}
Run Code Online (Sandbox Code Playgroud)
我通过使用Postman REST客户端测试了这个函数并获得了响应:
{
"type":"http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html",
"title":"Unprocessable Entity",
"status":422,
"detail":"Unable to delete entity."
}
Run Code Online (Sandbox Code Playgroud)
但是,当我检查mysql数据库时,正确删除了有问题的实体.没有出现错误的迹象.
返回这种错误的原因是什么?
更新:代码到达TableGateway删除函数调用后的行.这意味着响应可能是在调用函数之后构建的,并且忽略了返回的返回值.
小智 11
如果将删除逻辑修改为"return true",则API响应应按预期呈现HTTP 204.
...
class ItemResource extends AbstractResourceListener
{
...
public function delete($id)
{
$service = $this->serviceManager->get('...\ItemService');
$service->deleteItem($id);
return true;
}
...
}
Run Code Online (Sandbox Code Playgroud)