문제

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?

도움이 되었습니까?

해결책

You should have a look at this really complete blog post : http://williamdurand.fr/2012/08/02/rest-apis-with-symfony2-the-right-way/

William Durand talks about how to create a good REST Api and about JSON response with the correct status code.

If you are following what William wrote in his article, you will have a JSON response based on this format :

{
    code: 400,
    message: "Your message",
    first_data: "...",
    second_data: "...",
    ...
}

Which is not exactly what you are looking for. But I think the code your clients will have to write will be as simple as what you want with the format you proposed :

{
  meta: {
    code: 400,
    message: 'Your request failed because...',
  }
  data: {}
}

Maybe you can show what you did in your controller so my answer could be more precise.


Update:

With the following controller, you should be able to return the JSON Response you want :

use Symfony\Component\HttpFoundation\JsonResponse;

class MyController{
    //...
    public function myAction(){
        $myStatusCode = 400;

        return new JSonResponse(
            array('meta' => array('code' => $myStatusCode,
                                  'message' => 'Your request failed because...'),
                  'data' => array('your datas')
            ),
            $myStatusCode
        );
    }
    //...
}

This will create a response with the correct status code in the header and it allows you to add the message and status code you want in your JSon response.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top