Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
Redirect Class Reference
Inheritance diagram for Redirect:
RedirectInterface

Public Member Functions

 __construct (\Magento\Framework\App\RequestInterface $request, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\Encryption\UrlCoder $urlCoder, \Magento\Framework\Session\SessionManagerInterface $session, \Magento\Framework\Session\SidResolverInterface $sidResolver, \Magento\Framework\UrlInterface $urlBuilder, $canUseSessionIdInParam=true)
 
 getRefererUrl ()
 
 getRedirectUrl ($defaultUrl=null)
 
 error ($defaultUrl)
 
 success ($defaultUrl)
 
 updatePathParams (array $arguments)
 
 redirect (\Magento\Framework\App\ResponseInterface $response, $path, $arguments=[])
 
- Public Member Functions inherited from RedirectInterface
 redirect (\Magento\Framework\App\ResponseInterface $response, $path, $arguments=[])
 

Protected Member Functions

 _getUrl ()
 
 _isUrlInternal ($url)
 
 normalizeRefererUrl ($refererUrl)
 
 normalizeRefererQueryParts ($refererQuery)
 

Protected Attributes

 $_request
 
 $_storeManager
 
 $_urlCoder
 
 $_session
 
 $_sidResolver
 
 $_canUseSessionIdInParam
 
 $_urlBuilder
 

Additional Inherited Members

- Data Fields inherited from RedirectInterface
const PARAM_NAME_REFERER_URL = 'referer_url'
 
const PARAM_NAME_ERROR_URL = 'error_url'
 
const PARAM_NAME_SUCCESS_URL = 'success_url'
 

Detailed Description

Class Redirect computes redirect urls responses.

Definition at line 13 of file Redirect.php.

Constructor & Destructor Documentation

◆ __construct()

Constructor

Parameters
\Magento\Framework\App\RequestInterface$request
\Magento\Store\Model\StoreManagerInterface$storeManager
\Magento\Framework\Encryption\UrlCoder$urlCoder
\Magento\Framework\Session\SessionManagerInterface$session
\Magento\Framework\Session\SidResolverInterface$sidResolver
\Magento\Framework\UrlInterface$urlBuilder
bool$canUseSessionIdInParam

Definition at line 61 of file Redirect.php.

69  {
70  $this->_canUseSessionIdInParam = $canUseSessionIdInParam;
71  $this->_request = $request;
72  $this->_storeManager = $storeManager;
73  $this->_urlCoder = $urlCoder;
74  $this->_session = $session;
75  $this->_sidResolver = $sidResolver;
76  $this->_urlBuilder = $urlBuilder;
77  }
$storeManager

Member Function Documentation

◆ _getUrl()

_getUrl ( )
protected

Get the referrer url.

Returns
string
Exceptions

Definition at line 85 of file Redirect.php.

86  {
87  $refererUrl = $this->_request->getServer('HTTP_REFERER');
88  $encodedUrl = $this->_request->getParam(\Magento\Framework\App\ActionInterface::PARAM_NAME_URL_ENCODED)
89  ?: $this->_request->getParam(\Magento\Framework\App\ActionInterface::PARAM_NAME_BASE64_URL);
90 
91  if ($encodedUrl) {
92  $refererUrl = $this->_urlCoder->decode($encodedUrl);
93  } else {
94  $url = (string)$this->_request->getParam(self::PARAM_NAME_REFERER_URL);
95  if ($url) {
96  $refererUrl = $url;
97  }
98  }
99 
100  if (!$this->_isUrlInternal($refererUrl)) {
101  $refererUrl = $this->_storeManager->getStore()->getBaseUrl();
102  } else {
103  $refererUrl = $this->normalizeRefererUrl($refererUrl);
104  }
105  return $refererUrl;
106  }

◆ _isUrlInternal()

_isUrlInternal (   $url)
protected

Check whether URL is internal

Parameters
string$url
Returns
bool

Definition at line 197 of file Redirect.php.

198  {
199  if (strpos($url, 'http') !== false) {
201  $unsecureBaseUrl = $this->_storeManager->getStore()->getBaseUrl($directLinkType, false);
202  $secureBaseUrl = $this->_storeManager->getStore()->getBaseUrl($directLinkType, true);
203  return (strpos($url, $unsecureBaseUrl) === 0) || (strpos($url, $secureBaseUrl) === 0);
204  }
205  return false;
206  }

◆ error()

error (   $defaultUrl)

Redirect to error page

Parameters
string$defaultUrl
Returns
string

Implements RedirectInterface.

Definition at line 139 of file Redirect.php.

140  {
141  $errorUrl = $this->_request->getParam(self::PARAM_NAME_ERROR_URL);
142  if (empty($errorUrl)) {
143  $errorUrl = $defaultUrl;
144  }
145  if (!$this->_isUrlInternal($errorUrl)) {
146  $errorUrl = $this->_storeManager->getStore()->getBaseUrl();
147  }
148  return $errorUrl;
149  }

◆ getRedirectUrl()

getRedirectUrl (   $defaultUrl = null)

Set referer url for redirect in response

Parameters
string$defaultUrl
Returns
\Magento\Framework\App\ActionInterface

Implements RedirectInterface.

Definition at line 124 of file Redirect.php.

125  {
126  $refererUrl = $this->_getUrl();
127  if (empty($refererUrl)) {
128  $refererUrl = empty($defaultUrl) ? $this->_storeManager->getStore()->getBaseUrl() : $defaultUrl;
129  }
130  return $refererUrl;
131  }

◆ getRefererUrl()

getRefererUrl ( )

Identify referer url via all accepted methods (HTTP_REFERER, regular or base64-encoded request param)

Returns
string

Implements RedirectInterface.

Definition at line 113 of file Redirect.php.

114  {
115  return $this->_getUrl();
116  }

◆ normalizeRefererQueryParts()

normalizeRefererQueryParts (   $refererQuery)
protected

Normalize special parts of referer query

Parameters
array$refererQuery
Returns
array

Definition at line 246 of file Redirect.php.

247  {
248  $store = $this->_storeManager->getStore();
249 
250  if ($store
251  && !empty($refererQuery[\Magento\Store\Model\StoreManagerInterface::PARAM_NAME])
252  && ($refererQuery[\Magento\Store\Model\StoreManagerInterface::PARAM_NAME] !== $store->getCode())
253  ) {
255  }
256 
257  return $refererQuery;
258  }

◆ normalizeRefererUrl()

normalizeRefererUrl (   $refererUrl)
protected

Normalize path to avoid wrong store change

Parameters
string$refererUrl
Returns
string

Definition at line 214 of file Redirect.php.

215  {
216  if (!$refererUrl || !filter_var($refererUrl, FILTER_VALIDATE_URL)) {
217  return $refererUrl;
218  }
219 
220  $redirectParsedUrl = parse_url($refererUrl);
221  $refererQuery = [];
222 
223  if (!isset($redirectParsedUrl['query'])) {
224  return $refererUrl;
225  }
226 
227  parse_str($redirectParsedUrl['query'], $refererQuery);
228 
229  $refererQuery = $this->normalizeRefererQueryParts($refererQuery);
230  $normalizedUrl = $redirectParsedUrl['scheme']
231  . '://'
232  . $redirectParsedUrl['host']
233  . (isset($redirectParsedUrl['port']) ? ':' . $redirectParsedUrl['port'] : '')
234  . $redirectParsedUrl['path']
235  . ($refererQuery ? '?' . http_build_query($refererQuery) : '');
236 
237  return $normalizedUrl;
238  }
normalizeRefererQueryParts($refererQuery)
Definition: Redirect.php:246

◆ redirect()

redirect ( \Magento\Framework\App\ResponseInterface  $response,
  $path,
  $arguments = [] 
)

Set redirect into response

Parameters
\Magento\Framework\App\ResponseInterface$response
string$path
array$arguments
Returns
void

Definition at line 185 of file Redirect.php.

186  {
188  $response->setRedirect($this->_urlBuilder->getUrl($path, $arguments));
189  }
$response
Definition: 404.php:11
$arguments
updatePathParams(array $arguments)
Definition: Redirect.php:172

◆ success()

success (   $defaultUrl)

Redirect to success page

Parameters
string$defaultUrl
Returns
string

Implements RedirectInterface.

Definition at line 157 of file Redirect.php.

158  {
159  $successUrl = $this->_request->getParam(self::PARAM_NAME_SUCCESS_URL);
160  if (empty($successUrl)) {
161  $successUrl = $defaultUrl;
162  }
163  if (!$this->_isUrlInternal($successUrl)) {
164  $successUrl = $this->_storeManager->getStore()->getBaseUrl();
165  }
166  return $successUrl;
167  }

◆ updatePathParams()

updatePathParams ( array  $arguments)

Update path params for url builder

Parameters
array$arguments
Returns
array

Implements RedirectInterface.

Definition at line 172 of file Redirect.php.

173  {
174  return $arguments;
175  }
$arguments

Field Documentation

◆ $_canUseSessionIdInParam

$_canUseSessionIdInParam
protected

Definition at line 43 of file Redirect.php.

◆ $_request

$_request
protected

Definition at line 18 of file Redirect.php.

◆ $_session

$_session
protected

Definition at line 33 of file Redirect.php.

◆ $_sidResolver

$_sidResolver
protected

Definition at line 38 of file Redirect.php.

◆ $_storeManager

$_storeManager
protected

Definition at line 23 of file Redirect.php.

◆ $_urlBuilder

$_urlBuilder
protected

Definition at line 48 of file Redirect.php.

◆ $_urlCoder

$_urlCoder
protected

Definition at line 28 of file Redirect.php.


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