src/Controller/EntrepriseController.php line 18

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Entreprise;
  4. use App\Form\EntrepriseType;
  5. use App\Repository\CodeNafRepository;
  6. use App\Repository\EntrepriseRepository;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. #[Route('/entreprise')]
  12. class EntrepriseController extends AbstractController
  13. {
  14.     #[Route('/'name'app_entreprise_index'methods: ['GET'])]
  15.     public function index(EntrepriseRepository $entrepriseRepository): Response
  16.     {
  17.         return $this->render('entreprise/index.html.twig', [
  18.             'entreprises' => $entrepriseRepository->findAll(),
  19.         ]);
  20.     }
  21.     #[Route('/new'name'app_entreprise_new'methods: ['GET''POST'])]
  22.     public function new(Request $requestEntrepriseRepository $entrepriseRepository): Response
  23.     {
  24.         $entreprise = new Entreprise();
  25.         $form $this->createForm(EntrepriseType::class, $entreprise);
  26.         $form->handleRequest($request);
  27.         if ($form->isSubmitted() && $form->isValid()) {
  28.             $entrepriseRepository->save($entreprisetrue);
  29.             return $this->redirectToRoute('app_entreprise_edit', ["id"=>$entreprise->getId()], Response::HTTP_SEE_OTHER);
  30.         }
  31.         return $this->render('entreprise/new.html.twig', [
  32.             'entreprise' => $entreprise,
  33.             'form' => $form->createView(),
  34.         ]);
  35.     }
  36.     #[Route('/{id}'name'app_entreprise_show'methods: ['GET'])]
  37.     public function show(Entreprise $entrepriseCodeNafRepository $codeNafRepo): Response
  38.     {
  39.         $naf $codeNafRepo->findOneBy(["code"=>$entreprise->getNaf()]);
  40.         return $this->render('entreprise/show.html.twig', [
  41.             'entreprise' => $entreprise,
  42.             'naf' => $naf,
  43.         ]);
  44.     }
  45.     #[Route('/{id}/edit'name'app_entreprise_edit'methods: ['GET''POST'])]
  46.     public function edit(Request $requestEntreprise $entrepriseEntrepriseRepository $entrepriseRepositoryCodeNafRepository $codeNafRepo): Response
  47.     {
  48.         $form $this->createForm(EntrepriseType::class, $entreprise);
  49.         $form->handleRequest($request);
  50.         if ($form->isSubmitted() && $form->isValid()) {
  51.             $entrepriseRepository->save($entreprisetrue);
  52.             return $this->redirectToRoute('app_entreprise_show',  ["id"=>$entreprise->getId()], Response::HTTP_SEE_OTHER);
  53.         }
  54.         return $this->renderForm('entreprise/edit.html.twig', [
  55.             'entreprise' => $entreprise,
  56.             'form' => $form,
  57.         ]);
  58.     }
  59.     #[Route('/{id}'name'app_entreprise_delete'methods: ['POST'])]
  60.     public function delete(Request $requestEntreprise $entrepriseEntrepriseRepository $entrepriseRepository): Response
  61.     {
  62.         if ($this->isCsrfTokenValid('delete'.$entreprise->getId(), $request->request->get('_token'))) {
  63.             $entrepriseRepository->remove($entreprisetrue);
  64.         }
  65.         return $this->redirectToRoute('app_entreprise_index', [], Response::HTTP_SEE_OTHER);
  66.     }
  67. }