Skip to content

Commit 9b90dd2

Browse files
author
Yuri Kovsher
committed
MAGETWO-36731: Refactor controller actions in the Search module
1 parent 4f25bdd commit 9b90dd2

File tree

2 files changed

+31
-21
lines changed

2 files changed

+31
-21
lines changed
Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,54 @@
11
<?php
22
/**
3-
*
43
* Copyright © 2015 Magento. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
76
namespace Magento\Search\Controller\Ajax;
87

9-
use Magento\Search\Model\AutocompleteInterface;
8+
use Magento\Framework\App\Action\Action;
109
use Magento\Framework\App\Action\Context;
10+
use Magento\Search\Model\AutocompleteInterface;
11+
use Magento\Framework\Controller\ResultFactory;
1112

12-
class Suggest extends \Magento\Framework\App\Action\Action
13+
class Suggest extends Action
1314
{
1415
/**
15-
* @var AutocompleteInterface
16+
* @var \Magento\Search\Model\AutocompleteInterface
1617
*/
1718
private $autocomplete;
1819

1920
/**
20-
* @param Context $context
21-
* @param AutocompleteInterface $autocomplete
21+
* @param \Magento\Framework\App\Action\Context $context
22+
* @param \Magento\Search\Model\AutocompleteInterface $autocomplete
2223
*/
2324
public function __construct(
2425
Context $context,
2526
AutocompleteInterface $autocomplete
2627
) {
27-
parent::__construct($context);
2828
$this->autocomplete = $autocomplete;
29+
parent::__construct($context);
2930
}
3031

3132
/**
32-
* @return void
33+
* @return \Magento\Framework\Controller\ResultInterface
3334
*/
3435
public function execute()
3536
{
3637
if (!$this->getRequest()->getParam('q', false)) {
37-
$this->getResponse()->setRedirect($this->_url->getBaseUrl());
38-
return;
38+
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
39+
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
40+
$resultRedirect->setUrl($this->_url->getBaseUrl());
41+
return $resultRedirect;
3942
}
4043

4144
$autocompleteData = $this->autocomplete->getItems();
4245
$responseData = [];
4346
foreach ($autocompleteData as $resultItem) {
4447
$responseData[] = $resultItem->toArray();
4548
}
46-
$this->getResponse()->representJson(json_encode($responseData));
49+
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
50+
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
51+
$resultJson->setData($responseData);
52+
return $resultJson;
4753
}
4854
}

app/code/Magento/Search/Controller/Term/Popular.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,25 @@
55
*/
66
namespace Magento\Search\Controller\Term;
77

8+
use Magento\Framework\App\Action\Action;
89
use Magento\Framework\App\Action\Context;
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
911
use Magento\Framework\App\RequestInterface;
10-
use Magento\Framework\App\ResponseInterface;
12+
use Magento\Store\Model\ScopeInterface;
13+
use Magento\Framework\Controller\ResultFactory;
1114

12-
class Popular extends \Magento\Framework\App\Action\Action
15+
class Popular extends Action
1316
{
1417
/**
1518
* @var \Magento\Framework\App\Config\ScopeConfigInterface
1619
*/
1720
protected $scopeConfig;
1821

1922
/**
20-
* @param Context $context
23+
* @param \Magento\Framework\App\Action\Context $context
2124
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
2225
*/
23-
public function __construct(Context $context, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig)
26+
public function __construct(Context $context, ScopeConfigInterface $scopeConfig)
2427
{
2528
$this->scopeConfig = $scopeConfig;
2629
parent::__construct($context);
@@ -29,14 +32,14 @@ public function __construct(Context $context, \Magento\Framework\App\Config\Scop
2932
/**
3033
* Dispatch request
3134
*
32-
* @param RequestInterface $request
33-
* @return ResponseInterface
35+
* @param \Magento\Framework\App\RequestInterface $request
36+
* @return \Magento\Framework\App\ResponseInterface
3437
*/
3538
public function dispatch(RequestInterface $request)
3639
{
3740
$searchTerms = $this->scopeConfig->getValue(
3841
'catalog/seo/search_terms',
39-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
42+
ScopeInterface::SCOPE_STORE
4043
);
4144
if (!$searchTerms) {
4245
$this->_redirect('noroute');
@@ -46,11 +49,12 @@ public function dispatch(RequestInterface $request)
4649
}
4750

4851
/**
49-
* @return void
52+
* @return \Magento\Framework\View\Result\Page
5053
*/
5154
public function execute()
5255
{
53-
$this->_view->loadLayout();
54-
$this->_view->renderLayout();
56+
/** @var \Magento\Framework\View\Result\Page $resultPage */
57+
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
58+
return $resultPage;
5559
}
5660
}

0 commit comments

Comments
 (0)