hegresphere/src/Controller/EmployeeController.php
2024-11-28 17:56:10 +01:00

62 lines
2.1 KiB
PHP

<?php
namespace App\Controller;
use App\Entity\Employee;
use App\Form\EmployeeType;
use App\Repository\EmployeeRepository;
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;
#[Route('/employee')]
final class EmployeeController extends AbstractController
{
#[Route(name: 'app_employee_index', methods: ['GET'])]
public function index(EmployeeRepository $employeeRepository): Response
{
return $this->render('employee/index.html.twig', [
'employees' => $employeeRepository->findAll(),
]);
}
#[Route('/{id}', name: 'app_employee_show', methods: ['GET'])]
public function show(Employee $employee): Response
{
return $this->render('employee/show.html.twig', [
'employee' => $employee,
]);
}
#[Route('/{id}/edit', name: 'app_employee_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, Employee $employee, EntityManagerInterface $entityManager): Response
{
$form = $this->createForm(EmployeeType::class, $employee);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->flush();
return $this->redirectToRoute('app_employee_index', [], Response::HTTP_SEE_OTHER);
}
return $this->render('employee/edit.html.twig', [
'employee' => $employee,
'form' => $form,
]);
}
#[Route('/{id}', name: 'app_employee_delete', methods: ['POST'])]
public function delete(Request $request, Employee $employee, EntityManagerInterface $entityManager): Response
{
if ($this->isCsrfTokenValid('delete'.$employee->getId(), $request->getPayload()->getString('_token'))) {
$entityManager->remove($employee);
$entityManager->flush();
}
return $this->redirectToRoute('app_employee_index', [], Response::HTTP_SEE_OTHER);
}
}