Symfony2: JSON Response To Twig?
문제
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
.
제휴하지 않습니다 StackOverflow