I'm having trouble achieving something that should be simple using FOSRestBundle.
If I return an object, it works as intended. The JSON response will look something like
{
id: ...,
property: ...
}
What I'd like to do is return, on all requests and status codes, a envelope for the response, something like
{
meta: {
code: 200,
message: 'OK',
}
data: {
id: ...,
property: ...
}
}
This way, clients can write simple code to detect errors, where responses would look something like:
{
meta: {
code: 400,
message: 'Your request failed because...',
}
data: {}
}
I want to return this from multiple controllers, and return it only on JSON or XML requests. My first thought was to use a ResponseListener, check the format of the request, and modify the response if need be. Or, maybe, just set up a class something like
\Model\APIRequestFormatter, and from my controller, instead of doing a return $entity;
do return APIRequestFormatter->Format($entity);
Both of these approaches seem flawed to me, does anyone have any tips?