custom/plugins/MaxiaVariantsTable6/src/Storefront/Controller/VariantsTableController.php line 47

Open in your IDE?
  1. <?php
  2. namespace Maxia\MaxiaVariantsTable6\Storefront\Controller;
  3. use Maxia\MaxiaVariantsTable6\Core\Content\Product\SalesChannel\Detail\AbstractVariantsTableRoute;
  4. use Maxia\MaxiaVariantsTable6\Service\ConfigService;
  5. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  6. use Shopware\Core\PlatformRequest;
  7. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  8. use Shopware\Storefront\Controller\StorefrontController;
  9. use Shopware\Storefront\Framework\Cache\Annotation\HttpCache;
  10. use Shopware\Storefront\Framework\Routing\StorefrontResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. /**
  15.  * @RouteScope(scopes={"storefront"})
  16.  */
  17. class VariantsTableController extends StorefrontController
  18. {
  19.     /** @var ConfigService */
  20.     private $configService;
  21.     /** @var AbstractVariantsTableRoute */
  22.     private $variantsTableRoute;
  23.     public function __construct(ConfigService $configServiceAbstractVariantsTableRoute $variantsTableRoute)
  24.     {
  25.         $this->configService $configService;
  26.         $this->variantsTableRoute $variantsTableRoute;
  27.     }
  28.     /**
  29.      * @param Request $request
  30.      *
  31.      * @HttpCache()
  32.      * @RouteScope(scopes={"storefront"})
  33.      * @Route(
  34.      *     "/maxia-variants-table",
  35.      *     name="frontend.plugins.maxia-variants-table.ajax",
  36.      *     options={"seo"="false"},
  37.      *     defaults={"csrf_protected"=false, "XmlHttpRequest"=true},
  38.      *     methods={"GET"}
  39.      * )
  40.      */
  41.     public function indexAction(Request $request)
  42.     {
  43.         /** @var SalesChannelContext $context */
  44.         $context $request->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  45.         $config $this->configService->getPluginConfig($context);
  46.         if (!$config->isPluginEnabled()) {
  47.             throw new NotFoundHttpException();
  48.         }
  49.         $context->addExtension('maxiaVariantsTable'$config);
  50.         $variantsTableResponse $this->variantsTableRoute->load($request->get('productId'), $request$context);
  51.         if (!$variantsTableResponse || !$variantsTableResponse->getPagelet()) {
  52.             throw new NotFoundHttpException();
  53.         }
  54.         $response = new StorefrontResponse(json_encode($variantsTableResponse->getPagelet()->getBootstrapTableConfig()['data']));
  55.         $response->setContext($context);
  56.         $response->headers->set('Content-Type''application/json');
  57.         $response->headers->set('x-robots-tag''noindex');
  58.         return $response;
  59.     }
  60. }