src/Controller/ProductController.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Form\SearchType;
  4. use App\Repository\ProductRepository;
  5. use App\Model\Search;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. class ProductController extends AbstractController
  11. {
  12.     #[Route('/articles'name'product')]
  13.     public function index(ProductRepository $repositoryRequest $request): Response
  14.     {
  15.        
  16.         // Si recherche exécutée, $products contiendra les résultats filtrés
  17.         $search = new Search();
  18.         $form $this->createForm(SearchType::class, $search);
  19.         $form->handleRequest($request);
  20.         if ($form->isSubmitted() && $form->isValid()) {
  21.             $products $repository->findWithSearch($search);
  22.         } else {
  23.             $products $repository->findAll();
  24.         }
  25.         
  26.         return $this->renderForm('product/index.html.twig', [
  27.             'products' => $products,
  28.             'form' => $form,
  29.         ]);
  30.     }
  31.     #[Route('/articles/{slug}'name'product_show')]
  32.     public function show(ProductRepository $repositorystring $slug): Response
  33.     {
  34.         $product $repository->findOneBySlug($slug);
  35.         if (!$product) {
  36.             return $this->redirectToRoute('product');
  37.         }
  38.         return $this->render('product/show.html.twig', [
  39.             'product' => $product,
  40.         ]);
  41.     }
  42. }