src/Controller/Custom/SearchController.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Custom;
  3. use App\Repository\ProductRepository;
  4. use Sylius\Component\Channel\Context\ChannelContextInterface;
  5. use Sylius\Component\Locale\Context\LocaleContextInterface;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. class SearchController extends AbstractController
  10. {
  11.     protected ProductRepository $productRepository;
  12.     protected ChannelContextInterface $channelContext;
  13.     protected LocaleContextInterface $localeContext;
  14.     /**
  15.      * @param ProductRepository $productRepository
  16.      * @param ChannelContextInterface $channelContext
  17.      * @param LocaleContextInterface $localeContext
  18.      */
  19.     public function __construct(ProductRepository $productRepositoryChannelContextInterface $channelContextLocaleContextInterface $localeContext)
  20.     {
  21.         $this->productRepository $productRepository;
  22.         $this->channelContext $channelContext;
  23.         $this->localeContext $localeContext;
  24.     }
  25.     /**
  26.      * @param Request $request
  27.      * @return Response
  28.      */
  29.     public function search(Request $request): Response
  30.     {
  31.         $searchTerm $request->query->get('q');
  32.         $channel $this->channelContext->getChannel();
  33.         $localeCode $this->localeContext->getLocaleCode();
  34.         $total count($this->productRepository->findByTerm($channel$localeCode$searchTerm));
  35.         $products $this->productRepository->findByTerm($channel$localeCode$searchTerm3);
  36.         return $this->render('bundles/SyliusShopBundle/Product/Search/index.html.twig', [
  37.             'products' => $products,
  38.             'total' => $total,
  39.             'term' => $searchTerm
  40.         ]);
  41.     }
  42. }