src/Controller/ProduitController.php line 47

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Classe\Search;
  4. use App\Entity\Product;
  5. use App\Form\SearchType;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use App\Repository\ProductRepository;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. class ProduitController extends AbstractController
  13. {
  14.     private $entityManager;
  15.     public function __constructEntityManagerInterface $entityManager){
  16.         
  17.         $this->entityManager $entityManager;
  18.     }
  19.     #[Route('/nos-produits'name'app_produit')]
  20.    
  21.     public function index(ProductRepository $repo Request $request): Response
  22.     {
  23.         $products $repo->findAll();
  24.         $search = new Search();
  25.         $form $this->createForm(SearchType::class, $search);
  26.         $form->handleRequest($request);
  27.         if ($form->isSubmitted() && $form->isValid()) {
  28.             $products $repo->findWithSearch($search);
  29.            
  30.         }
  31.         return $this->render('produit/index.html.twig',[
  32.             'produits' => $products,
  33.             'form'=> $form->createView()
  34.         ]);
  35.     }
  36.     #[Route('/produit/{slug}'name'produit')]
  37.     public function show($slug ProductRepository $repo Request $request): Response
  38.     {
  39.         $product $repo->findOneBySlug($slug);
  40.         $products $repo->findByIsBest(1);
  41.         
  42.         
  43.         if(!$product){
  44.             return $this->redirectToRoute('app_produit');
  45.         }
  46.         
  47.         return $this->render('produit/show.html.twig',[
  48.             'produit' => $product,
  49.             'produits' => $products
  50.         ]);
  51.     }
  52. }