Manuel Hoffmann Manuel Hoffmann - 3 days ago 5
PHP Question

Apigility returning error even after successfull action

I am building a REST API using Apigility with Zend Framework 2.
In this API, I have a code-connected REST service and I am encountering a strange behaviour when I try to delete an entity.
I use the delete method of my TableGateway object to delete the data passed into the delete method of the "Resource" file:

public function delete($id)
{
//GetTable returns a TableGateway instance
$this->getTable('order')->delete(array('id' => $id));
return array("status" => "deleted", "id" => $id);
}


I tested this function by using the Postman REST client and got back the response:

{
"type":"http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html",
"title":"Unprocessable Entity",
"status":422,
"detail":"Unable to delete entity."
}


However, when I checked the mysql database, the entity in question was deleted properly.
There were no signs of an error occuring.

What could the cause of such an error being returned be?

Update:
The code reaches the lines after the TableGateway delete function call.
That means the response is probably built after the function is called and the return value I return is ignored.

Answer

If modify your delete logic to 'return true' then the API response should render a HTTP 204 as expected.

...
class ItemResource extends AbstractResourceListener
{
    ...
    public function delete($id)
    {
        $service = $this->serviceManager->get('...\ItemService');
        $service->deleteItem($id);
        return true;
    }
    ...
}
Comments