vendor/shopware/storefront/Controller/StorefrontController.php line 23

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Controller;
  3. use Shopware\Core\Checkout\Cart\Exception\CustomerNotLoggedInException;
  4. use Shopware\Core\Content\Seo\SeoUrlPlaceholderHandlerInterface;
  5. use Shopware\Core\Framework\Adapter\Twig\TemplateFinder;
  6. use Shopware\Core\Framework\Routing\RequestTransformerInterface;
  7. use Shopware\Core\PlatformRequest;
  8. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  9. use Shopware\Storefront\Event\StorefrontRenderEvent;
  10. use Shopware\Storefront\Framework\Routing\RequestTransformer;
  11. use Shopware\Storefront\Framework\Routing\Router;
  12. use Shopware\Storefront\Framework\Routing\StorefrontResponse;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\RequestStack;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\HttpKernel\EventListener\AbstractSessionListener;
  18. abstract class StorefrontController extends AbstractController
  19. {
  20.     protected function renderStorefront(string $view, array $parameters = []): Response
  21.     {
  22.         $request $this->get('request_stack')->getCurrentRequest();
  23.         $salesChannelContext $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  24.         $view $this->get(TemplateFinder::class)->find($viewfalsenull);
  25.         $event = new StorefrontRenderEvent($view$parameters$request$salesChannelContext);
  26.         $this->get('event_dispatcher')->dispatch($event);
  27.         $response $this->render($view$event->getParameters(), new StorefrontResponse());
  28.         if (!$response instanceof StorefrontResponse) {
  29.             throw new \RuntimeException('Symfony render implementation changed. Providing a response is no longer supported');
  30.         }
  31.         $host $request->attributes->get(RequestTransformer::SALES_CHANNEL_ABSOLUTE_BASE_URL)
  32.             . $request->attributes->get(RequestTransformer::SALES_CHANNEL_BASE_URL);
  33.         /** @var SeoUrlPlaceholderHandlerInterface $seoUrlReplacer */
  34.         $seoUrlReplacer $this->container->get(SeoUrlPlaceholderHandlerInterface::class);
  35.         $response->setContent(
  36.             $seoUrlReplacer->replace($response->getContent(), $host$salesChannelContext)
  37.         );
  38.         /* @var StorefrontResponse $response */
  39.         $response->setData($parameters);
  40.         $response->setContext($salesChannelContext);
  41.         $response->headers->set(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER'1');
  42.         $response->headers->set('Content-Type''text/html');
  43.         return $response;
  44.     }
  45.     protected function trans(string $snippet, array $parameters = []): string
  46.     {
  47.         return $this->container
  48.             ->get('translator')
  49.             ->trans($snippet$parameters);
  50.     }
  51.     protected function createActionResponse(Request $request): Response
  52.     {
  53.         if ($request->get('redirectTo')) {
  54.             $params $this->decodeParam($request'redirectParameters');
  55.             return $this->redirectToRoute($request->get('redirectTo'), $params);
  56.         }
  57.         if ($request->get('forwardTo')) {
  58.             $params $this->decodeParam($request'forwardParameters');
  59.             return $this->forwardToRoute($request->get('forwardTo'), [], $params);
  60.         }
  61.         return new Response();
  62.     }
  63.     protected function forwardToRoute(string $routeName, array $attributes = [], array $routeParameters = []): Response
  64.     {
  65.         $router $this->container->get('router');
  66.         $url $this->generateUrl($routeName$routeParametersRouter::PATH_INFO);
  67.         // for the route matching the request method is set to "GET" because
  68.         // this method is not ought to be used as a post passthrough
  69.         // rather it shall return templates or redirects to display results of the request ahead
  70.         $method $router->getContext()->getMethod();
  71.         $router->getContext()->setMethod(Request::METHOD_GET);
  72.         $route $router->match($url);
  73.         $router->getContext()->setMethod($method);
  74.         $request $this->container->get('request_stack')->getCurrentRequest();
  75.         $attributes array_merge(
  76.             $this->get(RequestTransformerInterface::class)->extractInheritableAttributes($request),
  77.             $route,
  78.             $attributes
  79.         );
  80.         return $this->forward($route['_controller'], $attributes$routeParameters);
  81.     }
  82.     /**
  83.      * @throws CustomerNotLoggedInException
  84.      */
  85.     protected function denyAccessUnlessLoggedIn(bool $allowGuest false): void
  86.     {
  87.         /** @var RequestStack $requestStack */
  88.         $requestStack $this->get('request_stack');
  89.         $request $requestStack->getCurrentRequest();
  90.         if (!$request) {
  91.             throw new CustomerNotLoggedInException();
  92.         }
  93.         /** @var SalesChannelContext|null $context */
  94.         $context $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  95.         if (
  96.             $context
  97.             && $context->getCustomer()
  98.             && (
  99.                 $allowGuest === true
  100.                 || $context->getCustomer()->getGuest() === false
  101.             )
  102.         ) {
  103.             return;
  104.         }
  105.         throw new CustomerNotLoggedInException();
  106.     }
  107.     protected function decodeParam(Request $requeststring $param): array
  108.     {
  109.         $params $request->get($param);
  110.         if (is_string($params)) {
  111.             $params json_decode($paramstrue);
  112.         }
  113.         if (empty($params)) {
  114.             $params = [];
  115.         }
  116.         return $params;
  117.     }
  118. }