문제

i'm getting a json response from mongodb, but i can't get this into a twig template. could somebody explain it and show best pratice? thanks.

/**
 * @Route("/event/{id}", name="event_details_view")
 * @Template()
 */
public function viewAction($id)
{
    $event = $this->get('doctrine.odm.mongodb.document_manager')
        ->getRepository('DungeonEventBundle:Event')
        ->findById($id);

    if (!$event) {
        throw $this->createNotFoundException('Event .$id. was not found.');
    }

    return new Response(json_encode($event));
}
도움이 되었습니까?

해결책

First of all, you are not getting a JSON response from MongoDB — you are getting an Event document object. If you want to pass it to Twig, instead of returning a response, return an array (since you're using the @Template annotation:

return array('event' => $event);

The object will be accessible in your template as event.

다른 팁

The best practice describe here would be to use a base.json.twig template, as described here and here, instead of Response(json_encode($data)).

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