collection-crud/src/Controller/GpuCoreController.php

90 lines
2.8 KiB
PHP

<?php
namespace App\Controller;
use App\Entity\GpuCore;
use App\Form\GPUCoreType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
#[Route('/gpu-core')]
class GpuCoreController extends AbstractController
{
#[Route('/', name: 'gpu-core_index', methods: ['GET'])]
public function index(EntityManagerInterface $entityManager): Response
{
$gPUCores = $entityManager
->getRepository(GpuCore::class)
->findBy([], [
'brand' => 'asc',
'architecture' => 'asc',
'name' => 'asc',
'variant' => 'asc',
]);
return $this->render('gpu_core/index.html.twig', [
'gpu_cores' => $gPUCores,
]);
}
#[Route('/new', name: 'gpu-core_new', methods: ['GET', 'POST'])]
public function new(Request $request, EntityManagerInterface $entityManager): Response
{
$gPUCore = new GpuCore();
$form = $this->createForm(GPUCoreType::class, $gPUCore);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->persist($gPUCore);
$entityManager->flush();
return $this->redirectToRoute('gpu-core_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('gpu_core/new.html.twig', [
'gpu_core' => $gPUCore,
'form' => $form,
]);
}
#[Route('/{id}', name: 'gpu-core_show', methods: ['GET'])]
public function show(GpuCore $gPUCore): Response
{
return $this->render('gpu_core/show.html.twig', [
'gpu_core' => $gPUCore,
]);
}
#[Route('/{id}/edit', name: 'gpu-core_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, GpuCore $gPUCore, EntityManagerInterface $entityManager): Response
{
$form = $this->createForm(GPUCoreType::class, $gPUCore);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->flush();
return $this->redirectToRoute('gpu-core_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('gpu_core/edit.html.twig', [
'gpu_core' => $gPUCore,
'form' => $form,
]);
}
#[Route('/{id}', name: 'gpu-core_delete', methods: ['POST'])]
public function delete(Request $request, GpuCore $gPUCore, EntityManagerInterface $entityManager): Response
{
if ($this->isCsrfTokenValid('delete'.$gPUCore->getId(), $request->request->get('_token'))) {
$entityManager->remove($gPUCore);
$entityManager->flush();
}
return $this->redirectToRoute('gpu-core_index', [], Response::HTTP_SEE_OTHER);
}
}