src/App/Controller/SearchController.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Section;
  4. use App\Form\Type\SearchType;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Sylius\Component\Channel\Context\ChannelContextInterface;
  7. use Sylius\Component\Core\Repository\ProductRepositoryInterface;
  8. use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. class SearchController extends AbstractController
  14. {
  15.     /**
  16.      * @Route("/search", name="search", methods={"POST"})
  17.      */
  18.     public function searchAction(
  19.         Request $request,
  20.         ProductRepositoryInterface $productRepository,
  21.         TaxonRepositoryInterface $taxonRepository,
  22.         EntityManagerInterface $entityManager,
  23.         ChannelContextInterface $channelContext
  24.     ): Response {
  25.         $form $this->createForm(SearchType::class);
  26.         $form->handleRequest($request);
  27.         if ($request->isMethod('POST') && $form->isSubmitted() && $form->isValid()) {
  28.             $data $form->getData();
  29.             $searchInput $data['search'];
  30.             $sections $entityManager->getRepository(Section::class)->getByTitleAndContent($searchInput);
  31.             $products $productRepository->findBySearchInput($searchInput$channelContext->getChannel());
  32.             $parentGroup $taxonRepository->findOneByCode('group');
  33.             
  34.             return $this->render('app/Search/results.html.twig', [
  35.                 'searchInput' => $searchInput,
  36.                 'sections'    => $sections,
  37.                 'products'    => $products,
  38.                 'parentGroup' => $parentGroup,
  39.             ]);
  40.         }
  41.         return $this->render('app/base/header/_search.html.twig', [
  42.             'form' => $form->createView(),
  43.         ]);
  44.     }
  45. }