Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Index.php
Go to the documentation of this file.
1 <?php
8 
17 
22 {
28  protected $_catalogSession;
29 
33  protected $_storeManager;
34 
38  private $_queryFactory;
39 
45  private $layerResolver;
46 
54  public function __construct(
55  Context $context,
56  Session $catalogSession,
58  QueryFactory $queryFactory,
59  Resolver $layerResolver
60  ) {
61  parent::__construct($context);
62  $this->_storeManager = $storeManager;
63  $this->_catalogSession = $catalogSession;
64  $this->_queryFactory = $queryFactory;
65  $this->layerResolver = $layerResolver;
66  }
67 
75  public function execute()
76  {
77  $this->layerResolver->create(Resolver::CATALOG_LAYER_SEARCH);
78 
79  /* @var $query \Magento\Search\Model\Query */
80  $query = $this->_queryFactory->get();
81 
82  $storeId = $this->_storeManager->getStore()->getId();
83  $query->setStoreId($storeId);
84 
85  $queryText = $query->getQueryText();
86 
87  if ($queryText != '') {
88  $catalogSearchHelper = $this->_objectManager->get(\Magento\CatalogSearch\Helper\Data::class);
89 
90  $getAdditionalRequestParameters = $this->getRequest()->getParams();
91  unset($getAdditionalRequestParameters[QueryFactory::QUERY_VAR_NAME]);
92 
93  if (empty($getAdditionalRequestParameters) &&
94  $this->_objectManager->get(PopularSearchTerms::class)->isCacheable($queryText, $storeId)
95  ) {
96  $this->getCacheableResult($catalogSearchHelper, $query);
97  } else {
98  $this->getNotCacheableResult($catalogSearchHelper, $query);
99  }
100  } else {
101  $this->getResponse()->setRedirect($this->_redirect->getRedirectUrl());
102  }
103  }
104 
112  private function getCacheableResult($catalogSearchHelper, $query)
113  {
114  if (!$catalogSearchHelper->isMinQueryLength()) {
115  $redirect = $query->getRedirect();
116  if ($redirect && $this->_url->getCurrentUrl() !== $redirect) {
117  $this->getResponse()->setRedirect($redirect);
118  return;
119  }
120  }
121 
122  $catalogSearchHelper->checkNotes();
123 
124  $this->_view->loadLayout();
125  $this->_view->renderLayout();
126  }
127 
137  private function getNotCacheableResult($catalogSearchHelper, $query)
138  {
139  if ($catalogSearchHelper->isMinQueryLength()) {
140  $query->setId(0)->setIsActive(1)->setIsProcessed(1);
141  } else {
142  $query->saveIncrementalPopularity();
143  $redirect = $query->getRedirect();
144  if ($redirect && $this->_url->getCurrentUrl() !== $redirect) {
145  $this->getResponse()->setRedirect($redirect);
146  return;
147  }
148  }
149 
150  $catalogSearchHelper->checkNotes();
151 
152  $this->_view->loadLayout();
153  $this->getResponse()->setNoCacheHeaders();
154  $this->_view->renderLayout();
155  }
156 }
_redirect($path, $arguments=[])
Definition: Action.php:167
$storeManager
__construct(Context $context, Session $catalogSession, StoreManagerInterface $storeManager, QueryFactory $queryFactory, Resolver $layerResolver)
Definition: Index.php:54