Advertisement
sanjiisan

Untitled

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