Skip to content

Commit 82a7ef2

Browse files
author
Dmytro Poperechnyy
committed
MAGETWO-34989: Implement getDefaultRedirect() method
1 parent 535a3aa commit 82a7ef2

File tree

7 files changed

+114
-4
lines changed

7 files changed

+114
-4
lines changed

app/code/Magento/Backend/App/Action/Context.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class Context extends \Magento\Framework\App\Action\Context
6868
* @param \Magento\Backend\Model\UrlInterface $backendUrl
6969
* @param \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
7070
* @param \Magento\Framework\Locale\ResolverInterface $localeResolver
71+
* @param \Magento\Backend\Model\View\Result\RedirectFactory $resultRedirectFactory
7172
* @param bool $canUseBaseUrl
7273
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
7374
*/
@@ -88,6 +89,7 @@ public function __construct(
8889
\Magento\Backend\Model\UrlInterface $backendUrl,
8990
\Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator,
9091
\Magento\Framework\Locale\ResolverInterface $localeResolver,
92+
\Magento\Backend\Model\View\Result\RedirectFactory $resultRedirectFactory,
9193
$canUseBaseUrl = false
9294
) {
9395
parent::__construct(
@@ -99,7 +101,8 @@ public function __construct(
99101
$redirect,
100102
$actionFlag,
101103
$view,
102-
$messageManager
104+
$messageManager,
105+
$resultRedirectFactory
103106
);
104107

105108
$this->_session = $session;

app/code/Magento/Backend/Model/View/Result/Redirect.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ public function __construct(
4242
parent::__construct($redirect, $urlBuilder);
4343
}
4444

45+
/**
46+
* Set referer url or dashboard if referer does not exist
47+
*
48+
* @return $this
49+
*/
50+
public function setRefererOrBaseUrl()
51+
{
52+
$this->url = $this->redirect->getRedirectUrl($this->urlBuilder->getUrl('adminhtml/index'));
53+
return $this;
54+
}
55+
4556
/**
4657
* {@inheritdoc}
4758
*/
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Backend\Model\View\Result;
7+
8+
use Magento\Framework\ObjectManagerInterface;
9+
10+
class RedirectFactory extends \Magento\Framework\Controller\Result\RedirectFactory
11+
{
12+
/**
13+
* Object Manager instance
14+
*
15+
* @var ObjectManagerInterface
16+
*/
17+
protected $objectManager;
18+
19+
/**
20+
* Instance name to create
21+
*
22+
* @var string
23+
*/
24+
protected $instanceName;
25+
26+
/**
27+
* @param ObjectManagerInterface $objectManager
28+
* @param string $instanceName
29+
*/
30+
public function __construct(
31+
ObjectManagerInterface $objectManager,
32+
$instanceName = 'Magento\Backend\Model\View\Result\Redirect'
33+
) {
34+
$this->objectManager = $objectManager;
35+
$this->instanceName = $instanceName;
36+
}
37+
38+
/**
39+
* Create class instance with specified parameters
40+
*
41+
* @param array $data
42+
* @return \Magento\Backend\Model\View\Result\Redirect
43+
*/
44+
public function create(array $data = [])
45+
{
46+
return $this->objectManager->create($this->instanceName, $data);
47+
}
48+
}

lib/internal/Magento/Framework/App/Action/AbstractAction.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,30 @@ abstract class AbstractAction implements \Magento\Framework\App\ActionInterface
1919
*/
2020
protected $_response;
2121

22+
/**
23+
* @var \Magento\Framework\App\Action\Context $context
24+
*/
25+
protected $context;
26+
27+
/**
28+
* @var \Magento\Framework\Controller\Result\RedirectFactory
29+
*/
30+
protected $resultRedirectFactory;
31+
2232
/**
2333
* @param \Magento\Framework\App\RequestInterface $request
2434
* @param \Magento\Framework\App\ResponseInterface $response
35+
* @param \Magento\Framework\App\Action\Context $context
2536
*/
2637
public function __construct(
2738
\Magento\Framework\App\RequestInterface $request,
28-
\Magento\Framework\App\ResponseInterface $response
39+
\Magento\Framework\App\ResponseInterface $response,
40+
\Magento\Framework\App\Action\Context $context
2941
) {
3042
$this->_request = $request;
3143
$this->_response = $response;
44+
$this->context = $context;
45+
$this->resultRedirectFactory = $context->getResultRedirectFactory();
3246
}
3347

3448
/**
@@ -50,4 +64,15 @@ public function getResponse()
5064
{
5165
return $this->_response;
5266
}
67+
68+
/**
69+
* Redirect user to the previous or main page
70+
*
71+
* @return \Magento\Framework\Controller\Result\Redirect|\Magento\Backend\Model\View\Result\Redirect
72+
*/
73+
public function getDefaultRedirect()
74+
{
75+
$resultRedirect = $this->resultRedirectFactory->create();
76+
return $resultRedirect->setRefererOrBaseUrl();
77+
}
5378
}

lib/internal/Magento/Framework/App/Action/Action.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class Action extends AbstractAction
6565
*/
6666
public function __construct(Context $context)
6767
{
68-
parent::__construct($context->getRequest(), $context->getResponse());
68+
parent::__construct($context->getRequest(), $context->getResponse(), $context);
6969
$this->_objectManager = $context->getObjectManager();
7070
$this->_eventManager = $context->getEventManager();
7171
$this->_url = $context->getUrl();

lib/internal/Magento/Framework/App/Action/Context.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ class Context implements \Magento\Framework\ObjectManager\ContextInterface
5252
*/
5353
protected $messageManager;
5454

55+
/**
56+
* @var \Magento\Framework\Controller\Result\RedirectFactory
57+
*/
58+
protected $resultRedirectFactory;
59+
5560
/**
5661
* @param \Magento\Framework\App\RequestInterface $request
5762
* @param \Magento\Framework\App\ResponseInterface $response
@@ -62,6 +67,7 @@ class Context implements \Magento\Framework\ObjectManager\ContextInterface
6267
* @param \Magento\Framework\App\ActionFlag $actionFlag
6368
* @param \Magento\Framework\App\ViewInterface $view
6469
* @param \Magento\Framework\Message\ManagerInterface $messageManager
70+
* @param \Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory
6571
*
6672
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
6773
*/
@@ -74,7 +80,8 @@ public function __construct(
7480
\Magento\Framework\App\Response\RedirectInterface $redirect,
7581
\Magento\Framework\App\ActionFlag $actionFlag,
7682
\Magento\Framework\App\ViewInterface $view,
77-
\Magento\Framework\Message\ManagerInterface $messageManager
83+
\Magento\Framework\Message\ManagerInterface $messageManager,
84+
\Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory
7885
) {
7986
$this->_request = $request;
8087
$this->_response = $response;
@@ -85,6 +92,7 @@ public function __construct(
8592
$this->_actionFlag = $actionFlag;
8693
$this->_view = $view;
8794
$this->messageManager = $messageManager;
95+
$this->resultRedirectFactory = $resultRedirectFactory;
8896
}
8997

9098
/**
@@ -158,4 +166,12 @@ public function getMessageManager()
158166
{
159167
return $this->messageManager;
160168
}
169+
170+
/**
171+
* @return \Magento\Framework\Controller\Result\RedirectFactory
172+
*/
173+
public function getResultRedirectFactory()
174+
{
175+
return $this->resultRedirectFactory;
176+
}
161177
}

lib/internal/Magento/Framework/App/ActionInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,11 @@ public function dispatch(RequestInterface $request);
3434
* @return ResponseInterface
3535
*/
3636
public function getResponse();
37+
38+
/**
39+
* Redirect to custom, previous or main page
40+
*
41+
* @return \Magento\Framework\Controller\ResultInterface
42+
*/
43+
public function getDefaultRedirect();
3744
}

0 commit comments

Comments
 (0)