|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- <?php
-
- namespace Lc\ShopBundle\Controller;
-
- use Doctrine\ORM\EntityManagerInterface;
- use Lc\ShopBundle\Context\CreditHistoryInterface;
- use Symfony\Bundle\FrameworkBundle\Controller\AbstractController as SfAbstractController;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\HttpFoundation\Response;
-
- abstract class AbstractController extends SfAbstractController
- {
- /**
- * @var \Doctrine\Persistence\ObjectManager
- * Entity manager
- */
- protected $em;
-
- public $class;
-
- public $type;
-
- public $template;
-
- public $routePrefix;
-
- public $repos;
-
-
- public function __construct(EntityManagerInterface $entityManager)
- {
- $this->em = $entityManager;
- $metadata = $this->em->getClassMetadata($this->class);
- $this->class = $metadata->getName();
- $this->repo = $this->em->getRepository($this->class);
-
- }
-
- public function indexAction(): Response
- {
- $repo = $this->em->getRepository($this->class);
- return $this->render('LcShopBundle:' . $this->template . ':index.html.twig', [
- 'entities' => $repo->findAll(),
- 'routePrefix'=> $this->routePrefix
- ]);
- }
-
- public function showAction($id): Response
- {
- $enity = $this->repo->find($id);
-
- return $this->render('LcShopBundle:'.$this->template.':show.html.twig', [
- 'entity' => $enity,
- 'routePrefix'=> $this->routePrefix
-
- ]);
- }
- public function editAction(Request $request, $id): Response
- {
-
- if ($id == 'new') {
- $entity = new $this->class;
- } else {
- $repo = $this->em->getRepository($this->class);
- $entity = $repo->find($id);
- }
- $form = $this->createForm($this->type, $entity);
- $form->handleRequest($request);
-
- if ($form->isSubmitted() && $form->isValid()) {
- $this->em->persist($entity);
- $this->getDoctrine()->getManager()->flush();
-
- return $this->redirectToRoute($this->routePrefix . '_index');
- }
-
-
- return $this->render('LcShopBundle:' . $this->template . ':edit.html.twig', [
- 'entity' => $entity,
- 'form' => $form->createView(),
- 'routePrefix'=> $this->routePrefix
- ]);
- }
-
-
- public function deleteAction(Request $request, $id): Response
- {
- $enity = $this->repo->find($id);
-
- if ($this->isCsrfTokenValid('delete'.$enity->getId(), $request->request->get('_token'))) {
- $entityManager = $this->getDoctrine()->getManager();
- $entityManager->remove($enity);
- $entityManager->flush();
- }
-
- return $this->redirectToRoute($this->routePrefix.'_index');
- }
-
- }
|