collection-crud/src/Controller/SocketController.php

85 lines
2.7 KiB
PHP
Raw Normal View History

2022-10-20 11:07:27 -04:00
<?php
namespace App\Controller;
use App\Entity\Socket;
use App\Form\SocketType;
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('/socket')]
class SocketController extends AbstractController
{
#[Route('/', name: 'socket_index', methods: ['GET'])]
public function index(EntityManagerInterface $entityManager): Response
{
$sockets = $entityManager
->getRepository(Socket::class)
->findAll();
return $this->render('socket/index.html.twig', [
'sockets' => $sockets,
]);
}
#[Route('/new', name: 'socket_new', methods: ['GET', 'POST'])]
public function new(Request $request, EntityManagerInterface $entityManager): Response
{
$socket = new Socket();
$form = $this->createForm(SocketType::class, $socket);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->persist($socket);
$entityManager->flush();
return $this->redirectToRoute('socket_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('socket/new.html.twig', [
'socket' => $socket,
'form' => $form,
]);
}
#[Route('/{id}', name: 'socket_show', methods: ['GET'])]
public function show(Socket $socket): Response
{
return $this->render('socket/show.html.twig', [
'socket' => $socket,
]);
}
#[Route('/{id}/edit', name: 'socket_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, Socket $socket, EntityManagerInterface $entityManager): Response
{
$form = $this->createForm(SocketType::class, $socket);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->flush();
return $this->redirectToRoute('socket_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('socket/edit.html.twig', [
'socket' => $socket,
'form' => $form,
]);
}
#[Route('/{id}', name: 'socket_delete', methods: ['POST'])]
public function delete(Request $request, Socket $socket, EntityManagerInterface $entityManager): Response
{
if ($this->isCsrfTokenValid('delete'.$socket->getId(), $request->request->get('_token'))) {
$entityManager->remove($socket);
$entityManager->flush();
}
return $this->redirectToRoute('socket_index', [], Response::HTTP_SEE_OTHER);
}
}