<?php
namespace App\Controller;
use App\Classe\Search;
use App\Entity\Product;
use App\Form\SearchType;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use App\Repository\ProductRepository;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class ProduitController extends AbstractController
{
private $entityManager;
public function __construct( EntityManagerInterface $entityManager){
$this->entityManager = $entityManager;
}
#[Route('/nos-produits', name: 'app_produit')]
public function index(ProductRepository $repo , Request $request): Response
{
$products = $repo->findAll();
$search = new Search();
$form = $this->createForm(SearchType::class, $search);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$products = $repo->findWithSearch($search);
}
return $this->render('produit/index.html.twig',[
'produits' => $products,
'form'=> $form->createView()
]);
}
#[Route('/produit/{slug}', name: 'produit')]
public function show($slug , ProductRepository $repo , Request $request): Response
{
$product = $repo->findOneBySlug($slug);
$products = $repo->findByIsBest(1);
if(!$product){
return $this->redirectToRoute('app_produit');
}
return $this->render('produit/show.html.twig',[
'produit' => $product,
'produits' => $products
]);
}
}