vendor/shopware/storefront/Controller/SearchController.php line 57

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Controller;
  3. use Shopware\Core\Content\Product\SalesChannel\Search\AbstractProductSearchRoute;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  5. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  6. use Shopware\Core\Framework\Routing\Annotation\Since;
  7. use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
  8. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  9. use Shopware\Storefront\Framework\Cache\Annotation\HttpCache;
  10. use Shopware\Storefront\Page\Search\SearchPageLoadedHook;
  11. use Shopware\Storefront\Page\Search\SearchPageLoader;
  12. use Shopware\Storefront\Page\Search\SearchWidgetLoadedHook;
  13. use Shopware\Storefront\Page\Suggest\SuggestPageLoadedHook;
  14. use Shopware\Storefront\Page\Suggest\SuggestPageLoader;
  15. use Symfony\Component\HttpFoundation\JsonResponse;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. /**
  20.  * @Route(defaults={"_routeScope"={"storefront"}})
  21.  */
  22. class SearchController extends StorefrontController
  23. {
  24.     /**
  25.      * @var SearchPageLoader
  26.      */
  27.     private $searchPageLoader;
  28.     /**
  29.      * @var SuggestPageLoader
  30.      */
  31.     private $suggestPageLoader;
  32.     /**
  33.      * @var AbstractProductSearchRoute
  34.      */
  35.     private $productSearchRoute;
  36.     public function __construct(
  37.         SearchPageLoader $searchPageLoader,
  38.         SuggestPageLoader $suggestPageLoader,
  39.         AbstractProductSearchRoute $productSearchRoute
  40.     ) {
  41.         $this->searchPageLoader $searchPageLoader;
  42.         $this->suggestPageLoader $suggestPageLoader;
  43.         $this->productSearchRoute $productSearchRoute;
  44.     }
  45.     /**
  46.      * @Since("6.0.0.0")
  47.      * @HttpCache()
  48.      * @Route("/search", name="frontend.search.page", methods={"GET"})
  49.      */
  50.     public function search(SalesChannelContext $contextRequest $request): Response
  51.     {
  52.         try {
  53.             $page $this->searchPageLoader->load($request$context);
  54.             if ($page->getListing()->getTotal() === 1) {
  55.                 $product $page->getListing()->first();
  56.                 if ($request->get('search') === $product->getProductNumber()) {
  57.                     $productId $product->getId();
  58.                     return $this->forwardToRoute('frontend.detail.page', [], ['productId' => $productId]);
  59.                 }
  60.             }
  61.         } catch (MissingRequestParameterException $missingRequestParameterException) {
  62.             return $this->forwardToRoute('frontend.home.page');
  63.         }
  64.         $this->hook(new SearchPageLoadedHook($page$context));
  65.         return $this->renderStorefront('@Storefront/storefront/page/search/index.html.twig', ['page' => $page]);
  66.     }
  67.     /**
  68.      * @Since("6.0.0.0")
  69.      * @HttpCache()
  70.      * @Route("/suggest", name="frontend.search.suggest", methods={"GET"}, defaults={"XmlHttpRequest"=true})
  71.      */
  72.     public function suggest(SalesChannelContext $contextRequest $request): Response
  73.     {
  74.         $page $this->suggestPageLoader->load($request$context);
  75.         $this->hook(new SuggestPageLoadedHook($page$context));
  76.         return $this->renderStorefront('@Storefront/storefront/layout/header/search-suggest.html.twig', ['page' => $page]);
  77.     }
  78.     /**
  79.      * @Since("6.2.0.0")
  80.      * @HttpCache()
  81.      *
  82.      * Route to load the listing filters
  83.      *
  84.      * @Route("/widgets/search", name="widgets.search.pagelet.v2", methods={"GET", "POST"}, defaults={"XmlHttpRequest"=true, "_routeScope"={"storefront"}})
  85.      */
  86.     public function ajax(Request $requestSalesChannelContext $context): Response
  87.     {
  88.         $request->request->set('no-aggregations'true);
  89.         $page $this->searchPageLoader->load($request$context);
  90.         $this->hook(new SearchWidgetLoadedHook($page$context));
  91.         $response $this->renderStorefront('@Storefront/storefront/page/search/search-pagelet.html.twig', ['page' => $page]);
  92.         $response->headers->set('x-robots-tag''noindex');
  93.         return $response;
  94.     }
  95.     /**
  96.      * @Since("6.3.3.0")
  97.      * @HttpCache()
  98.      *
  99.      * Route to load the available listing filters
  100.      *
  101.      * @Route("/widgets/search/filter", name="widgets.search.filter", methods={"GET", "POST"}, defaults={"XmlHttpRequest"=true, "_routeScope"={"storefront"}})
  102.      */
  103.     public function filter(Request $requestSalesChannelContext $context): Response
  104.     {
  105.         $term $request->get('search');
  106.         if (!$term) {
  107.             throw new MissingRequestParameterException('search');
  108.         }
  109.         // Allows to fetch only aggregations over the gateway.
  110.         $request->request->set('only-aggregations'true);
  111.         // Allows to convert all post-filters to filters. This leads to the fact that only aggregation values are returned, which are combinable with the previous applied filters.
  112.         $request->request->set('reduce-aggregations'true);
  113.         $criteria = new Criteria();
  114.         $criteria->setTitle('search-page');
  115.         $result $this->productSearchRoute
  116.             ->load($request$context$criteria)
  117.             ->getListingResult();
  118.         $mapped = [];
  119.         foreach ($result->getAggregations() as $aggregation) {
  120.             $mapped[$aggregation->getName()] = $aggregation;
  121.         }
  122.         $response = new JsonResponse($mapped);
  123.         $response->headers->set('x-robots-tag''noindex');
  124.         return $response;
  125.     }
  126. }