Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace CoderslabBundle\Controller;
- use CoderslabBundle\Entity\Book;
- use Symfony\Bundle\FrameworkBundle\Controller\Controller;
- use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\HttpFoundation\Response;
- class DefaultController extends Controller
- {
- /**
- * @Route("/newBook/", name="newBook")
- */
- public function newBookAction()
- {
- return $this->render('CoderslabBundle:Form:form.html.twig');
- }
- /**
- * @Route("/createBook/", name="createBook")
- */
- public function createBookAction(Request $request)
- {
- $title = $request->request->get('title');
- $description = $request->request->get('description');
- $rating = $request->request->get('rating');
- $newBook = new Book();
- $newBook->setTitle($title);
- $newBook->setDescription($description);
- $newBook->setRating($rating);
- $em = $this->getDoctrine()->getEntityManager();
- $em->persist($newBook);
- $em->flush();
- return $this->redirect($this->generateUrl('showBook', ['id' => $newBook->getId()]));
- }
- /**
- * @Route("/showBook/{id}", name="showBook")
- */
- public function showBookAction($id)
- {
- $em = $this->getDoctrine()->getEntityManager();
- $bookRepository = $em->getRepository('CoderslabBundle:Book');
- $bookObj = $bookRepository->find($id);
- if ($bookObj == null) {
- die('Nie ma');
- }
- return $this->render('CoderslabBundle:Book:showBook.html.twig', [
- 'book' => $bookObj
- ]);
- }
- /**
- * @Route("/showAllBooks/", name="showAllBooks")
- */
- public function showAllBooksAction()
- {
- $em = $this->getDoctrine()->getEntityManager();
- $bookRepository = $em->getRepository('CoderslabBundle:Book');
- $allBooks = $bookRepository->findAll();
- return $this->render('CoderslabBundle:Book:showAllBooks.html.twig', [
- 'allBooks' => $allBooks
- ]);
- }
- /**
- * @Route("/removeBook/{id}", name="removeBook")
- */
- public function removeBookAction($id)
- {
- $em = $this->getDoctrine()->getEntityManager();
- $bookRepository = $em->getRepository('CoderslabBundle:Book');
- $book = $bookRepository->find($id);
- if ($book == null) {
- die('Nie ma');
- }
- $em->remove($book);
- $em->flush();
- return new Response('Udało się usunąć');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement