0

I've set up FOSRestBundle with my small Symfony2 website. I've created some small GET methods and when I visit the api url, I get my JSON data. Perfect. But I'm trying to build an iphone app and I use RestKit for mapping/parsing json/... The problem I'm experiencing is that it has to be Key-value coding (KVC) JSON data.

I use this basic code in my ApiController to retrieve all data from my Sports database table:

public function getSportsAction() {
    $sports = $this->getDoctrine()
            ->getRepository('MatchTrackerAppBundle:Sports')
            ->findAll();

    $view = View::create()
            ->setStatusCode(200)
            ->setData($sports);

    return $this->get('fos_rest.view_handler')->handle($view);
}

At the moment I get this output:

[
    {
        "id": 1,
        "name": "Voetbal"
    },
    {
        "id": 2,
        "name": "Tennis"
    }
]

It doesn't have a key! How can I add a key in front of it. What I want is:

{
    "sports": [
    {
            "id": 1,
            "name": "Voetbal"
        },
        {
            "id": 2,
            "name": "Tennis"
        }
    ]
}

Is this possible with FOSRestBundle?

Jesse
  • 879
  • 1
  • 12
  • 26

1 Answers1

1

Shot in the dark, as I've never used this bundle... but would this work?

$view = View::create()
        ->setStatusCode(200)
        ->setData(  array("sports" => $sports)  );
Thomas Kelley
  • 10,187
  • 1
  • 36
  • 43
  • This works! Excellent. Had a feeling that it would be something easy, but all the Code & json samples on FosRestBundle don't use key-value coding, so I was clueless. Thanks – Jesse Dec 04 '12 at 11:32