Url.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model;
  7. use Magento\Framework\App\Config\ScopeConfigInterface;
  8. use Magento\Framework\App\RequestInterface;
  9. use Magento\Framework\Url\EncoderInterface;
  10. use Magento\Framework\UrlInterface;
  11. use Magento\Store\Model\ScopeInterface;
  12. /**
  13. * Customer url model
  14. */
  15. class Url
  16. {
  17. /**
  18. * Route for customer account login page
  19. */
  20. const ROUTE_ACCOUNT_LOGIN = 'customer/account/login';
  21. /**
  22. * Config name for Redirect Customer to Account Dashboard after Logging in setting
  23. */
  24. const XML_PATH_CUSTOMER_STARTUP_REDIRECT_TO_DASHBOARD = 'customer/startup/redirect_dashboard';
  25. /**
  26. * Query param name for last url visited
  27. */
  28. const REFERER_QUERY_PARAM_NAME = 'referer';
  29. /**
  30. * @var UrlInterface
  31. */
  32. protected $urlBuilder;
  33. /**
  34. * @var RequestInterface
  35. */
  36. protected $request;
  37. /**
  38. * @var ScopeConfigInterface
  39. */
  40. protected $scopeConfig;
  41. /**
  42. * @var Session
  43. */
  44. protected $customerSession;
  45. /**
  46. * @var EncoderInterface
  47. */
  48. protected $urlEncoder;
  49. /**
  50. * @var \Magento\Framework\Url\DecoderInterface
  51. */
  52. private $urlDecoder;
  53. /**
  54. * @var \Magento\Framework\Url\HostChecker
  55. */
  56. private $hostChecker;
  57. /**
  58. * @param Session $customerSession
  59. * @param ScopeConfigInterface $scopeConfig
  60. * @param RequestInterface $request
  61. * @param UrlInterface $urlBuilder
  62. * @param EncoderInterface $urlEncoder
  63. * @param \Magento\Framework\Url\DecoderInterface|null $urlDecoder
  64. * @param \Magento\Framework\Url\HostChecker|null $hostChecker
  65. */
  66. public function __construct(
  67. Session $customerSession,
  68. ScopeConfigInterface $scopeConfig,
  69. RequestInterface $request,
  70. UrlInterface $urlBuilder,
  71. EncoderInterface $urlEncoder,
  72. \Magento\Framework\Url\DecoderInterface $urlDecoder = null,
  73. \Magento\Framework\Url\HostChecker $hostChecker = null
  74. ) {
  75. $this->request = $request;
  76. $this->urlBuilder = $urlBuilder;
  77. $this->scopeConfig = $scopeConfig;
  78. $this->customerSession = $customerSession;
  79. $this->urlEncoder = $urlEncoder;
  80. $this->urlDecoder = $urlDecoder ?: \Magento\Framework\App\ObjectManager::getInstance()
  81. ->get(\Magento\Framework\Url\DecoderInterface::class);
  82. $this->hostChecker = $hostChecker ?: \Magento\Framework\App\ObjectManager::getInstance()
  83. ->get(\Magento\Framework\Url\HostChecker::class);
  84. }
  85. /**
  86. * Retrieve customer login url
  87. *
  88. * @return string
  89. */
  90. public function getLoginUrl()
  91. {
  92. return $this->urlBuilder->getUrl(self::ROUTE_ACCOUNT_LOGIN, $this->getLoginUrlParams());
  93. }
  94. /**
  95. * Retrieve parameters of customer login url
  96. *
  97. * @return array
  98. */
  99. public function getLoginUrlParams()
  100. {
  101. $params = [];
  102. $referer = $this->getRequestReferrer();
  103. if (!$referer
  104. && !$this->scopeConfig->isSetFlag(
  105. self::XML_PATH_CUSTOMER_STARTUP_REDIRECT_TO_DASHBOARD,
  106. ScopeInterface::SCOPE_STORE
  107. )
  108. && !$this->customerSession->getNoReferer()
  109. ) {
  110. $referer = $this->urlBuilder->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true]);
  111. $referer = $this->urlEncoder->encode($referer);
  112. }
  113. if ($referer) {
  114. $params = [self::REFERER_QUERY_PARAM_NAME => $referer];
  115. }
  116. return $params;
  117. }
  118. /**
  119. * Retrieve customer login POST URL
  120. *
  121. * @return string
  122. */
  123. public function getLoginPostUrl()
  124. {
  125. $params = [];
  126. $referer = $this->getRequestReferrer();
  127. if ($referer) {
  128. $params = [
  129. self::REFERER_QUERY_PARAM_NAME => $referer,
  130. ];
  131. }
  132. return $this->urlBuilder->getUrl('customer/account/loginPost', $params);
  133. }
  134. /**
  135. * Retrieve customer logout url
  136. *
  137. * @return string
  138. */
  139. public function getLogoutUrl()
  140. {
  141. return $this->urlBuilder->getUrl('customer/account/logout');
  142. }
  143. /**
  144. * Retrieve customer dashboard url
  145. *
  146. * @return string
  147. */
  148. public function getDashboardUrl()
  149. {
  150. return $this->urlBuilder->getUrl('customer/account');
  151. }
  152. /**
  153. * Retrieve customer account page url
  154. *
  155. * @return string
  156. */
  157. public function getAccountUrl()
  158. {
  159. return $this->urlBuilder->getUrl('customer/account');
  160. }
  161. /**
  162. * Retrieve customer register form url
  163. *
  164. * @return string
  165. */
  166. public function getRegisterUrl()
  167. {
  168. return $this->urlBuilder->getUrl('customer/account/create');
  169. }
  170. /**
  171. * Retrieve customer register form post url
  172. *
  173. * @return string
  174. */
  175. public function getRegisterPostUrl()
  176. {
  177. return $this->urlBuilder->getUrl('customer/account/createpost');
  178. }
  179. /**
  180. * Retrieve customer account edit form url
  181. *
  182. * @return string
  183. */
  184. public function getEditUrl()
  185. {
  186. return $this->urlBuilder->getUrl('customer/account/edit');
  187. }
  188. /**
  189. * Retrieve customer edit POST URL
  190. *
  191. * @return string
  192. */
  193. public function getEditPostUrl()
  194. {
  195. return $this->urlBuilder->getUrl('customer/account/editpost');
  196. }
  197. /**
  198. * Retrieve url of forgot password page
  199. *
  200. * @return string
  201. */
  202. public function getForgotPasswordUrl()
  203. {
  204. return $this->urlBuilder->getUrl('customer/account/forgotpassword');
  205. }
  206. /**
  207. * Retrieve confirmation URL for Email
  208. *
  209. * @param string $email
  210. * @return string
  211. */
  212. public function getEmailConfirmationUrl($email = null)
  213. {
  214. return $this->urlBuilder->getUrl('customer/account/confirmation', ['_query' => ['email' => $email]]);
  215. }
  216. /**
  217. * @return mixed|null
  218. */
  219. private function getRequestReferrer()
  220. {
  221. $referer = $this->request->getParam(self::REFERER_QUERY_PARAM_NAME);
  222. if ($referer && $this->hostChecker->isOwnOrigin($this->urlDecoder->decode($referer))) {
  223. return $referer;
  224. }
  225. return null;
  226. }
  227. }