/** * @Route("/comment", name="Comment") */ public function commentAction(Request $request) { $user = $this->getUser(); $comment = new Comment(); $now = new DateTime(); $doodle_id = $request->get('id'); $text = $request->get('comment'); $doodle = $this->getDoctrine()->getRepository('AppBundle:Doodle')->find($doodle_id); $comment->setUser($user); $comment->setDoodle($doodle); $comment->setCreated($now); $comment->setText($text); if ($user) { $comment->setAuthor($user->username); } else { $comment->setAuthor('Anonymous'); } $em = $this->getDoctrine()->getManager(); $em->persist($comment); $em->flush(); $data = ['message' => 'Success!', 'id' => $doodle_id, 'text' => $text]; $response = new Response(json_encode(['response' => $data])); $response->headers->set('Content-Type', 'application/json'); return $response; }