Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Redirect.php
Go to the documentation of this file.
1 <?php
7 
9 use Magento\Customer\Model\Url as CustomerUrl;
22 
26 class Redirect
27 {
29  const LOGIN_REDIRECT_URL = 'login_redirect';
30 
34  protected $request;
35 
39  protected $scopeConfig;
40 
44  protected $storeManager;
45 
49  protected $urlDecoder;
50 
54  protected $customerUrl;
55 
60  protected $url;
61 
65  protected $resultFactory;
66 
70  protected $cookieManager;
71 
75  private $hostChecker;
76 
80  private $session;
81 
93  public function __construct(
95  Session $customerSession,
100  CustomerUrl $customerUrl,
102  HostChecker $hostChecker = null
103  ) {
104  $this->request = $request;
105  $this->session = $customerSession;
106  $this->scopeConfig = $scopeConfig;
107  $this->storeManager = $storeManager;
108  $this->url = $url;
109  $this->urlDecoder = $urlDecoder;
110  $this->customerUrl = $customerUrl;
111  $this->resultFactory = $resultFactory;
112  $this->hostChecker = $hostChecker ?: ObjectManager::getInstance()->get(HostChecker::class);
113  }
114 
120  public function getRedirect()
121  {
122  $this->updateLastCustomerId();
123  $this->prepareRedirectUrl();
124 
126  if ($this->session->getBeforeRequestParams()) {
127  $result = $this->resultFactory->create(ResultFactory::TYPE_FORWARD);
128  $result->setParams($this->session->getBeforeRequestParams())
129  ->setModule($this->session->getBeforeModuleName())
130  ->setController($this->session->getBeforeControllerName())
131  ->forward($this->session->getBeforeAction());
132  } else {
133  $result = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
134  $result->setUrl($this->session->getBeforeAuthUrl(true));
135  }
136  return $result;
137  }
138 
144  protected function updateLastCustomerId()
145  {
146  $lastCustomerId = $this->session->getLastCustomerId();
147  if (isset($lastCustomerId)
148  && $this->session->isLoggedIn()
149  && $lastCustomerId != $this->session->getId()
150  ) {
151  $this->session->unsBeforeAuthUrl()
152  ->setLastCustomerId($this->session->getId());
153  }
154  }
155 
161  protected function prepareRedirectUrl()
162  {
163  $baseUrl = $this->storeManager->getStore()->getBaseUrl();
164 
165  $url = $this->session->getBeforeAuthUrl();
166  if (!$url) {
167  $url = $baseUrl;
168  }
169 
170  switch ($url) {
171  case $baseUrl:
172  if ($this->session->isLoggedIn()) {
173  $this->processLoggedCustomer();
174  } else {
175  $this->applyRedirect($this->customerUrl->getLoginUrl());
176  }
177  break;
178 
179  case $this->customerUrl->getLogoutUrl():
180  $this->applyRedirect($this->customerUrl->getDashboardUrl());
181  break;
182 
183  default:
184  if (!$this->session->getAfterAuthUrl()) {
185  $this->session->setAfterAuthUrl($this->session->getBeforeAuthUrl());
186  }
187  if ($this->session->isLoggedIn()) {
188  $this->applyRedirect($this->session->getAfterAuthUrl(true));
189  }
190  break;
191  }
192  }
193 
201  protected function processLoggedCustomer()
202  {
203  // Set default redirect URL for logged in customer
204  $this->applyRedirect($this->customerUrl->getAccountUrl());
205 
206  if (!$this->scopeConfig->isSetFlag(
207  CustomerUrl::XML_PATH_CUSTOMER_STARTUP_REDIRECT_TO_DASHBOARD,
209  )
210  ) {
211  $referer = $this->request->getParam(CustomerUrl::REFERER_QUERY_PARAM_NAME);
212  if ($referer) {
213  $referer = $this->urlDecoder->decode($referer);
214  preg_match('/logoutSuccess\//', $referer, $matches, PREG_OFFSET_CAPTURE);
215  if (!empty($matches)) {
216  $referer = str_replace('logoutSuccess/', '', $referer);
217  }
218  if ($this->hostChecker->isOwnOrigin($referer)) {
219  $this->applyRedirect($referer);
220  }
221  }
222  } elseif ($this->session->getAfterAuthUrl()) {
223  $this->applyRedirect($this->session->getAfterAuthUrl(true));
224  }
225  }
226 
233  private function applyRedirect($url)
234  {
235  $this->session->setBeforeAuthUrl($url);
236  }
237 
244  protected function getCookieManager()
245  {
246  if (!is_object($this->cookieManager)) {
247  $this->cookieManager = ObjectManager::getInstance()->get(CookieManagerInterface::class);
248  }
249  return $this->cookieManager;
250  }
251 
259  public function setCookieManager($value)
260  {
261  $this->cookieManager = $value;
262  }
263 
269  public function getRedirectCookie()
270  {
271  return $this->getCookieManager()->getCookie(self::LOGIN_REDIRECT_URL, null);
272  }
273 
280  public function setRedirectCookie($route)
281  {
282  $this->getCookieManager()->setPublicCookie(self::LOGIN_REDIRECT_URL, $route);
283  }
284 
290  public function clearRedirectCookie()
291  {
292  $this->getCookieManager()->deleteCookie(self::LOGIN_REDIRECT_URL);
293  }
294 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$value
Definition: gender.phtml:16
__construct(RequestInterface $request, Session $customerSession, ScopeConfigInterface $scopeConfig, StoreManagerInterface $storeManager, UrlInterface $url, DecoderInterface $urlDecoder, CustomerUrl $customerUrl, ResultFactory $resultFactory, HostChecker $hostChecker=null)
Definition: Redirect.php:93