Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
View.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
10 use Magento\Framework\App\Action\HttpGetActionInterface as HttpGetActionInterface;
13 use Magento\Catalog\Controller\Product as ProductAction;
14 
18 class View extends ProductAction implements HttpGetActionInterface, HttpPostActionInterface
19 {
23  protected $viewHelper;
24 
29 
33  protected $resultPageFactory;
34 
43  public function __construct(
44  Context $context,
45  \Magento\Catalog\Helper\Product\View $viewHelper,
46  \Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory,
48  ) {
49  $this->viewHelper = $viewHelper;
50  $this->resultForwardFactory = $resultForwardFactory;
51  $this->resultPageFactory = $resultPageFactory;
52  parent::__construct($context);
53  }
54 
60  protected function noProductRedirect()
61  {
62  $store = $this->getRequest()->getQuery('store');
63  if (isset($store) && !$this->getResponse()->isRedirect()) {
64  $resultRedirect = $this->resultRedirectFactory->create();
65  return $resultRedirect->setPath('');
66  } elseif (!$this->getResponse()->isRedirect()) {
67  $resultForward = $this->resultForwardFactory->create();
68  $resultForward->forward('noroute');
69  return $resultForward;
70  }
71  }
72 
78  public function execute()
79  {
80  // Get initial data from request
81  $categoryId = (int) $this->getRequest()->getParam('category', false);
82  $productId = (int) $this->getRequest()->getParam('id');
83  $specifyOptions = $this->getRequest()->getParam('options');
84 
85  if ($this->getRequest()->isPost() && $this->getRequest()->getParam(self::PARAM_NAME_URL_ENCODED)) {
86  $product = $this->_initProduct();
87 
88  if (!$product) {
89  return $this->noProductRedirect();
90  }
91 
92  if ($specifyOptions) {
93  $notice = $product->getTypeInstance()->getSpecifyOptionMessage();
94  $this->messageManager->addNoticeMessage($notice);
95  }
96 
97  if ($this->getRequest()->isAjax()) {
98  $this->getResponse()->representJson(
99  $this->_objectManager->get(\Magento\Framework\Json\Helper\Data::class)->jsonEncode([
100  'backUrl' => $this->_redirect->getRedirectUrl()
101  ])
102  );
103  return;
104  }
105  $resultRedirect = $this->resultRedirectFactory->create();
106  $resultRedirect->setRefererOrBaseUrl();
107  return $resultRedirect;
108  }
109 
110  // Prepare helper and params
111  $params = new \Magento\Framework\DataObject();
112  $params->setCategoryId($categoryId);
113  $params->setSpecifyOptions($specifyOptions);
114 
115  // Render page
116  try {
117  $page = $this->resultPageFactory->create();
118  $this->viewHelper->prepareAndRender($page, $productId, $this, $params);
119  return $page;
120  } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
121  return $this->noProductRedirect();
122  } catch (\Exception $e) {
123  $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
124  $resultForward = $this->resultForwardFactory->create();
125  $resultForward->forward('noroute');
126  return $resultForward;
127  }
128  }
129 }
_redirect($path, $arguments=[])
Definition: Action.php:167
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$page
Definition: pages.php:8
__construct(Context $context, \Magento\Catalog\Helper\Product\View $viewHelper, \Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory, PageFactory $resultPageFactory)
Definition: View.php:43
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18