/** * @return void */ public function execute() { if (!$this->getRequest()->getParam('q', false)) { $this->getResponse()->setRedirect($this->_url->getBaseUrl()); return; } $autocompleteData = $this->autocomplete->getItems(); $responseData = []; foreach ($autocompleteData as $resultItem) { $responseData[] = $resultItem->toArray(); } $this->getResponse()->representJson(json_encode($responseData)); }
/** * @return \Magento\Framework\Controller\ResultInterface */ public function execute() { if (!$this->getRequest()->getParam('q', false)) { /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */ $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); $resultRedirect->setUrl($this->_url->getBaseUrl()); return $resultRedirect; } $autocompleteData = $this->autocomplete->getItems(); $responseData = []; foreach ($autocompleteData as $resultItem) { $responseData[] = $resultItem->toArray(); } /** @var \Magento\Framework\Controller\Result\Json $resultJson */ $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON); $resultJson->setData($responseData); return $resultJson; }