You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractController.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace Lc\ShopBundle\Controller;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\ShopBundle\Context\CreditHistoryInterface;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController as SfAbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. abstract class AbstractController extends SfAbstractController
  9. {
  10. /**
  11. * @var \Doctrine\Persistence\ObjectManager
  12. * Entity manager
  13. */
  14. protected $em;
  15. public $class;
  16. public $type;
  17. public $template;
  18. public $routePrefix;
  19. public $repos;
  20. public function __construct(EntityManagerInterface $entityManager)
  21. {
  22. $this->em = $entityManager;
  23. $metadata = $this->em->getClassMetadata($this->class);
  24. $this->class = $metadata->getName();
  25. $this->repo = $this->em->getRepository($this->class);
  26. }
  27. public function indexAction(): Response
  28. {
  29. $repo = $this->em->getRepository($this->class);
  30. return $this->render('LcShopBundle:' . $this->template . ':index.html.twig', [
  31. 'entities' => $repo->findAll(),
  32. 'routePrefix'=> $this->routePrefix
  33. ]);
  34. }
  35. public function showAction($id): Response
  36. {
  37. $enity = $this->repo->find($id);
  38. return $this->render('LcShopBundle:'.$this->template.':show.html.twig', [
  39. 'entity' => $enity,
  40. 'routePrefix'=> $this->routePrefix
  41. ]);
  42. }
  43. public function editAction(Request $request, $id): Response
  44. {
  45. if ($id == 'new') {
  46. $entity = new $this->class;
  47. } else {
  48. $repo = $this->em->getRepository($this->class);
  49. $entity = $repo->find($id);
  50. }
  51. $form = $this->createForm($this->type, $entity);
  52. $form->handleRequest($request);
  53. if ($form->isSubmitted() && $form->isValid()) {
  54. $this->em->persist($entity);
  55. $this->getDoctrine()->getManager()->flush();
  56. return $this->redirectToRoute($this->routePrefix . '_index');
  57. }
  58. return $this->render('LcShopBundle:' . $this->template . ':edit.html.twig', [
  59. 'entity' => $entity,
  60. 'form' => $form->createView(),
  61. 'routePrefix'=> $this->routePrefix
  62. ]);
  63. }
  64. public function deleteAction(Request $request, $id): Response
  65. {
  66. $enity = $this->repo->find($id);
  67. if ($this->isCsrfTokenValid('delete'.$enity->getId(), $request->request->get('_token'))) {
  68. $entityManager = $this->getDoctrine()->getManager();
  69. $entityManager->remove($enity);
  70. $entityManager->flush();
  71. }
  72. return $this->redirectToRoute($this->routePrefix.'_index');
  73. }
  74. }