/
forest-lynx
/
project
Обзор
Документация
Войти
/
forest-lynx
/
project
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/Controller/CrudController.php
129 строк
4 KB
forest-lynx
init
04 дек 2024, 10:44
04 дек 2024, 10:44
f4ceba2
Код
Авторство
О чём код?
<?php namespace App\Controller; use App\Controller\Trait\HasWhenAction; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; abstract class CrudController extends AbstractController { use HasWhenAction; private const ROUTE_SUFFIX_INDEX = '.index'; private ?array $otherData; public function __construct( protected EntityManagerInterface $entityManager ) { } abstract protected function getTitle(): string; abstract protected function getEntityClass(): string; abstract protected function getFormTypeClass(): string; abstract protected function getTemplate(string $action): string; abstract protected function getRoutePrefix(): string; protected function getOtherData(): array { return $this->otherData ?? []; } protected function setOtherData(array $data): static { $this->otherData = $data; return $this; } #[Route('/', name: '.index', defaults: ['page' => 1], methods: [Request::METHOD_GET])] public function index(Request $request, int $page): Response { $entities = $this->entityManager->getRepository($this->getEntityClass())->findAll(); return $this->render($this->getTemplate('index'), [ 'page_title' => $this->getTitle(), 'entities' => $entities, ]); } #[Route('/create', name: '.create', methods: [Request::METHOD_GET,Request::METHOD_POST])] public function create(Request $request): Response { $entity = new ($this->getEntityClass())(); $form = $this->createForm($this->getFormTypeClass(), $entity); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $this->entityManager->persist($entity); $this->entityManager->flush(); $this->whenCreating($request, $entity); $this->addFlash('success', 'message.created_successfully'); return $this->redirectToRoute($this->getRoutePrefix() . '.edit', ['id' => $entity->getId()], Response::HTTP_SEE_OTHER); } return $this->render('default/create.html.twig', [ 'entity' => $entity, 'form' => $form, ]); } #[Route('/{id}/edit', name: '.edit', methods: [Request::METHOD_GET,Request::METHOD_POST])] public function edit(Request $request, mixed $id): Response { $entity = $this->entityManager->getRepository($this->getEntityClass())->find($id); if (!$entity) { throw $this->createNotFoundException(); } $form = $this->createForm($this->getFormTypeClass(), $entity); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $this->entityManager->flush(); $this->addFlash('success', 'message.updated_successfully'); return $this->redirectToRoute($this->getRoutePrefix() . '.edit', ['id' => $id], Response::HTTP_SEE_OTHER); } $this->whenEditing($request, $entity); return $this->render( $this->getTemplate('edit'), [ 'entity' => $entity, 'form' => $form, ...$this->getOtherData(), ] ); } #[Route('/{id}', name: '.delete', methods: [Request::METHOD_POST])] public function delete(Request $request, mixed $id): Response { /** @var string|null $token */ $token = $request->getPayload()->get('token'); if ($this->isCsrfTokenValid('delete', $token)) { $entity = $this->entityManager->getRepository($this->getEntityClass())->find($id); if (!$entity) { throw $this->createNotFoundException(); } $this->entityManager->remove($entity); $this->entityManager->flush(); $this->whenDeleting($request, $entity); $this->addFlash('success', 'message.deleted_successfully'); } return $this->redirectToRoute($this->getRoutePrefix() . self::ROUTE_SUFFIX_INDEX, [], Response::HTTP_SEE_OTHER); } }