vendor/shopware/storefront/Controller/CmsController.php line 60

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Controller;
  3. use Shopware\Core\Content\Category\Exception\CategoryNotFoundException;
  4. use Shopware\Core\Content\Category\SalesChannel\AbstractCategoryRoute;
  5. use Shopware\Core\Content\Cms\Exception\PageNotFoundException;
  6. use Shopware\Core\Content\Cms\SalesChannel\AbstractCmsRoute;
  7. use Shopware\Core\Content\Product\SalesChannel\Listing\ProductListingGateway;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  9. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  10. use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
  11. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  12. use Shopware\Storefront\Framework\Cache\Annotation\HttpCache;
  13. use Symfony\Component\HttpFoundation\JsonResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. /**
  18.  * @RouteScope(scopes={"storefront"})
  19.  */
  20. class CmsController extends StorefrontController
  21. {
  22.     /**
  23.      * @var ProductListingGateway
  24.      */
  25.     private $listingGateway;
  26.     /**
  27.      * @var AbstractCmsRoute
  28.      */
  29.     private $cmsRoute;
  30.     /**
  31.      * @var AbstractCategoryRoute
  32.      */
  33.     private $categoryRoute;
  34.     public function __construct(
  35.         ProductListingGateway $listingGateway,
  36.         AbstractCmsRoute $cmsRoute,
  37.         AbstractCategoryRoute $categoryRoute
  38.     ) {
  39.         $this->listingGateway $listingGateway;
  40.         $this->cmsRoute $cmsRoute;
  41.         $this->categoryRoute $categoryRoute;
  42.     }
  43.     /**
  44.      * Route for cms data (used in XmlHttpRequest)
  45.      *
  46.      * @HttpCache()
  47.      * @Route("/widgets/cms/{id}", name="frontend.cms.page", methods={"GET", "POST"}, defaults={"id"=null, "XmlHttpRequest"=true})
  48.      *
  49.      * @throws InconsistentCriteriaIdsException
  50.      * @throws MissingRequestParameterException
  51.      * @throws PageNotFoundException
  52.      */
  53.     public function page(string $idRequest $requestSalesChannelContext $salesChannelContext): Response
  54.     {
  55.         if (!$id) {
  56.             throw new MissingRequestParameterException('Parameter id missing');
  57.         }
  58.         $cmsPage $this->cmsRoute->load($id$request$salesChannelContext)->getCmsPage();
  59.         return $this->renderStorefront('@Storefront/storefront/page/content/detail.html.twig', ['cmsPage' => $cmsPage]);
  60.     }
  61.     /**
  62.      * Route to load a cms page which assigned to the provided navigation id.
  63.      * Navigation id is required to load the slot config for the navigation
  64.      *
  65.      * @Route("/widgets/cms/navigation/{navigationId}", name="frontend.cms.navigation.page", methods={"GET", "POST"}, defaults={"navigationId"=null, "XmlHttpRequest"=true})
  66.      *
  67.      * @throws CategoryNotFoundException
  68.      * @throws MissingRequestParameterException
  69.      * @throws PageNotFoundException
  70.      * @throws InconsistentCriteriaIdsException
  71.      */
  72.     public function category(string $navigationIdRequest $requestSalesChannelContext $salesChannelContext): Response
  73.     {
  74.         if (!$navigationId) {
  75.             throw new MissingRequestParameterException('Parameter navigationId missing');
  76.         }
  77.         $category $this->categoryRoute->load($navigationId$request$salesChannelContext)->getCategory();
  78.         if (!$category->getCmsPageId()) {
  79.             throw new PageNotFoundException('');
  80.         }
  81.         return $this->renderStorefront('@Storefront/storefront/page/content/detail.html.twig', ['cmsPage' => $category->getCmsPage()]);
  82.     }
  83.     /**
  84.      * @HttpCache()
  85.      *
  86.      * Route to load the listing filters
  87.      *
  88.      * @RouteScope(scopes={"storefront"})
  89.      * @Route("/widgets/cms/navigation/{navigationId}/filter", name="frontend.cms.navigation.filter", methods={"GET", "POST"}, defaults={"XmlHttpRequest"=true})
  90.      *
  91.      * @throws MissingRequestParameterException
  92.      */
  93.     public function filter(string $navigationIdRequest $requestSalesChannelContext $context): Response
  94.     {
  95.         if (!$navigationId) {
  96.             throw new MissingRequestParameterException('Parameter navigationId missing');
  97.         }
  98.         // Allows to fetch only aggregations over the gateway.
  99.         $request->request->set('only-aggregations'true);
  100.         // 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.
  101.         $request->request->set('reduce-aggregations'true);
  102.         $listing $this->listingGateway->search($request$context);
  103.         $mapped = [];
  104.         foreach ($listing->getAggregations() as $aggregation) {
  105.             $mapped[$aggregation->getName()] = $aggregation;
  106.         }
  107.         return new JsonResponse($mapped);
  108.     }
  109. }