Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
Redirect Class Reference

Public Member Functions

 __construct (RequestInterface $request, Session $customerSession, ScopeConfigInterface $scopeConfig, StoreManagerInterface $storeManager, UrlInterface $url, DecoderInterface $urlDecoder, CustomerUrl $customerUrl, ResultFactory $resultFactory, HostChecker $hostChecker=null)
 
 setCookieManager ($value)
 
 getRedirectCookie ()
 
 setRedirectCookie ($route)
 
 clearRedirectCookie ()
 

Data Fields

const LOGIN_REDIRECT_URL = 'login_redirect'
 

Protected Member Functions

 updateLastCustomerId ()
 
 prepareRedirectUrl ()
 
 processLoggedCustomer ()
 
 getCookieManager ()
 

Protected Attributes

 $request
 
 $scopeConfig
 
 $storeManager
 
 $urlDecoder
 
 $customerUrl
 
 $url
 
 $resultFactory
 
 $cookieManager
 

Detailed Description

@SuppressWarnings(PHPMD.CouplingBetweenObjects)

Definition at line 26 of file Redirect.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( RequestInterface  $request,
Session  $customerSession,
ScopeConfigInterface  $scopeConfig,
StoreManagerInterface  $storeManager,
UrlInterface  $url,
DecoderInterface  $urlDecoder,
CustomerUrl  $customerUrl,
ResultFactory  $resultFactory,
HostChecker  $hostChecker = null 
)
Parameters
RequestInterface$request
Session$customerSession
ScopeConfigInterface$scopeConfig
StoreManagerInterface$storeManager
UrlInterface$url
DecoderInterface$urlDecoder
CustomerUrl$customerUrl
ResultFactory$resultFactory
HostChecker | null$hostChecker

Definition at line 93 of file Redirect.php.

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  }

Member Function Documentation

◆ clearRedirectCookie()

clearRedirectCookie ( )

Clear cookie with requested route

Returns
void

Definition at line 290 of file Redirect.php.

291  {
292  $this->getCookieManager()->deleteCookie(self::LOGIN_REDIRECT_URL);
293  }

◆ getCookieManager()

getCookieManager ( )
protected

Get Cookie manager. For release backward compatibility.

Deprecated:
100.0.10
Returns
CookieManagerInterface

Definition at line 244 of file Redirect.php.

245  {
246  if (!is_object($this->cookieManager)) {
247  $this->cookieManager = ObjectManager::getInstance()->get(CookieManagerInterface::class);
248  }
249  return $this->cookieManager;
250  }

◆ getRedirectCookie()

getRedirectCookie ( )

Get redirect route from cookie for case of successful login/registration

Returns
null|string

Definition at line 269 of file Redirect.php.

270  {
271  return $this->getCookieManager()->getCookie(self::LOGIN_REDIRECT_URL, null);
272  }

◆ prepareRedirectUrl()

prepareRedirectUrl ( )
protected

Prepare redirect URL

Returns
void

Definition at line 161 of file Redirect.php.

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  }

◆ processLoggedCustomer()

processLoggedCustomer ( )
protected

Prepare redirect URL for logged in customer

Redirect customer to the last page visited after logging in.

Returns
void

Definition at line 201 of file Redirect.php.

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  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17

◆ setCookieManager()

setCookieManager (   $value)

Set cookie manager. For unit tests.

Deprecated:
100.0.10
Parameters
object$value
Returns
void

Definition at line 259 of file Redirect.php.

260  {
261  $this->cookieManager = $value;
262  }
$value
Definition: gender.phtml:16

◆ setRedirectCookie()

setRedirectCookie (   $route)

Save redirect route to cookie for case of successful login/registration

Parameters
string$route
Returns
void

Definition at line 280 of file Redirect.php.

281  {
282  $this->getCookieManager()->setPublicCookie(self::LOGIN_REDIRECT_URL, $route);
283  }

◆ updateLastCustomerId()

updateLastCustomerId ( )
protected

Update last customer id, if required

Returns
void

Definition at line 144 of file Redirect.php.

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  }

Field Documentation

◆ $cookieManager

$cookieManager
protected

Definition at line 70 of file Redirect.php.

◆ $customerUrl

$customerUrl
protected

Definition at line 54 of file Redirect.php.

◆ $request

$request
protected

Definition at line 34 of file Redirect.php.

◆ $resultFactory

$resultFactory
protected

Definition at line 65 of file Redirect.php.

◆ $scopeConfig

$scopeConfig
protected

Definition at line 39 of file Redirect.php.

◆ $storeManager

$storeManager
protected

Definition at line 44 of file Redirect.php.

◆ $url

$url
protected

Definition at line 60 of file Redirect.php.

◆ $urlDecoder

$urlDecoder
protected

Definition at line 49 of file Redirect.php.

◆ LOGIN_REDIRECT_URL

const LOGIN_REDIRECT_URL = 'login_redirect'

URL to redirect user on successful login or registration

Definition at line 29 of file Redirect.php.


The documentation for this class was generated from the following file: