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

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