Advertisement
sanjiisan

Untitled

Aug 5th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. <?php
  2.  
  3. namespace AppBundle\Controller;
  4.  
  5. use AppBundle\Entity\User;
  6. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  7. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use AppBundle\Entity\Book;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  12.  
  13. class DefaultController extends Controller
  14. {
  15. /**
  16. * @Route("/newBook", name="new_book")
  17. * @return Response
  18. */
  19. public function newBookAction()
  20. {
  21. return $this->render('@App/Default/form.html.twig');
  22. }
  23.  
  24. /**
  25. * @Route("/createBook", name="create_book")
  26. */
  27. public function createBookAction(Request $request)
  28. {
  29. $book = new Book();
  30. $book->setDescription($request->request->get('description'));
  31. $book->setTitle($request->request->get('title'));
  32. $book->setRating($request->request->get('rating'));
  33.  
  34. $em = $this->getDoctrine()->getEntityManager();
  35.  
  36. $em->persist($book);
  37. $em->flush();
  38.  
  39. return $this->redirectToRoute('show_book', ['id' => $book->getId()]);
  40. }
  41.  
  42. /**
  43. * @Route("/showBook/{id}", name="show_book")
  44. */
  45. public function showBookAction($id)
  46. {
  47. $em = $this->getDoctrine()->getEntityManager();
  48. $repository = $em->getRepository('AppBundle:Book');
  49.  
  50. $book = $repository->find($id);
  51.  
  52. return $this->render('@App/Default/render.html.twig', [
  53. 'book' => $book
  54. ]);
  55. }
  56.  
  57. /**
  58. * @Route("/showAllBooks")
  59. */
  60. public function showAllBooksAction()
  61. {
  62. $em = $this->getDoctrine()->getEntityManager();
  63. $repository = $em->getRepository('AppBundle:Book');
  64. $books = $repository->findAll();
  65.  
  66. return $this->render('@App/Default/renderAll.html.twig', [
  67. 'books' => $books
  68. ]);
  69. }
  70.  
  71.  
  72. /**
  73. * @Route("/deleteBook/{id}")
  74. */
  75. public function deleteBooksAction($id)
  76. {
  77. $em = $this->getDoctrine()->getEntityManager();
  78. $repository = $em->getRepository('AppBundle:Book');
  79.  
  80. $book = $repository->find($id);
  81. $em->remove($book);
  82. $em->flush();
  83.  
  84. return new Response('Usunięto!!');
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement