/** * @Route("/api/book") * @Method("POST") */ public function createAction(Request $request) { $user = $this->getUserByToken($request); if (array_key_exists('error', $user)) { return $this->createApiResponse($user, 200); } $book = new Book(); $book->setOwner($user); $form = $this->createForm(new BookType(), $book, array('is_api' => true, 'is_owner_disabled' => true)); $this->processForm($request, $form); $em = $this->getDoctrine()->getManager(); // $this->denyAccessUnlessGranted('create', $book, 'Unauthorized access!'); $em->persist($book); $em->flush(); $bookUrl = $this->generateUrl('api_book_get', ['id' => $book->getId()]); $response = $this->createApiResponse($book, 201); $response->headers->set('Location', $bookUrl); return $response; }
/** * @Route("/book/create", name="book_create") * @Template("BookBundle:Book:_form.html.twig") */ public function createAction(Request $request) { $user = $this->checkUser(); if (!$user) { return new RedirectResponse($this->generateUrl('index')); } $em = $this->getDoctrine()->getManager(); $book = new Book(); $book->setOwner($user); $this->denyAccessUnlessGranted('create', $book, 'Unauthorized access!'); $form = $this->createForm(new BookType(), $book, array('is_owner_disabled' => true)); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $data = $form->getData(); $book->setTitle($data->getTitle()); $book->setDescription($data->getDescription()); $em->persist($book); $em->flush(); $this->flashMessage(array('alert' => 'success', 'title' => 'Success!', 'message' => 'Successfully created Book id: #' . $book->getId() . '!')); return new RedirectResponse($this->generateUrl('book_list')); } return array('create' => true, 'book' => $book, 'form' => $form->createView()); }