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

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