Request.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /*
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecshop\services;
  10. //use fecshop\models\mongodb\UrlRewrite;
  11. use Yii;
  12. use yii\base\InvalidConfigException;
  13. /**
  14. * rewrite class \yii\web\Request
  15. * use custom url in our system, example: www.example.com/xxxxx.html, this file is not
  16. * exit in our system, In order to consider SEO, we can use db storage map between custom url and yii url
  17. * when request visit /xxxx.html, select this custom url in mongodb, return the yii url ,ex. /product/index?_id=3
  18. * then , resolve /product/index?_id=3 .
  19. * @author Terry Zhao <2358269014@qq.com>
  20. * @since 1.0
  21. */
  22. class Request extends \yii\web\Request
  23. {
  24. protected $_urlRewriteModelName = '\fecshop\models\mongodb\UrlRewrite';
  25. protected $_urlRewriteModel;
  26. public function init()
  27. {
  28. parent::init();
  29. list($this->_urlRewriteModelName, $this->_urlRewriteModel) = \Yii::mapGet($this->_urlRewriteModelName);
  30. }
  31. /**
  32. * rewrite yii\web\Request resolveRequestUri().
  33. */
  34. protected function resolveRequestUri()
  35. {
  36. if (isset($_SERVER['HTTP_X_REWRITE_URL'])) { // IIS
  37. $requestUri = $_SERVER['HTTP_X_REWRITE_URL'];
  38. } elseif (isset($_SERVER['REQUEST_URI'])) {
  39. $requestUri = $_SERVER['REQUEST_URI'];
  40. if ($requestUri !== '' && $requestUri[0] !== '/') {
  41. $requestUri = preg_replace('/^(http|https):\/\/[^\/]+/i', '', $requestUri);
  42. }
  43. } elseif (isset($_SERVER['ORIG_PATH_INFO'])) { // IIS 5.0 CGI
  44. $requestUri = $_SERVER['ORIG_PATH_INFO'];
  45. if (!empty($_SERVER['QUERY_STRING'])) {
  46. $requestUri .= '?' . $_SERVER['QUERY_STRING'];
  47. }
  48. } else {
  49. throw new InvalidConfigException('Unable to determine the request URI.');
  50. }
  51. /*
  52. * Replace Code
  53. * //return $requestUri;
  54. * To:
  55. */
  56. return $this->getRewriteUri($requestUri);
  57. }
  58. /**
  59. * get module request url by db ;.
  60. */
  61. protected function getRewriteUri($requestUri)
  62. {
  63. $baseUrl = $this->getBaseUrl();
  64. $requestUriRelative = $requestUri;
  65. if ($baseUrl) {
  66. $requestUriRelative = substr($requestUriRelative, strlen($baseUrl));
  67. }
  68. //echo $requestUriRelative;exit;
  69. $urlKey = '';
  70. $urlParam = '';
  71. $urlParamSuffix = '';
  72. if (strstr($requestUriRelative, '#')) {
  73. list($urlNoSuffix, $urlParamSuffix) = explode('#', $requestUriRelative);
  74. if (strstr($urlNoSuffix, '?')) {
  75. list($urlKey, $urlParam) = explode('?', $urlNoSuffix);
  76. }
  77. } elseif (strstr($requestUriRelative, '?')) {
  78. list($urlKey, $urlParam) = explode('?', $requestUriRelative);
  79. } else {
  80. $urlKey = $requestUriRelative;
  81. }
  82. if ($urlParamSuffix) {
  83. $urlParamSuffix = '#'.$urlParamSuffix;
  84. }
  85. if ($originUrlPath = Yii::$app->url->getOriginUrl($urlKey)) {
  86. if (strstr($originUrlPath, '?')) {
  87. if ($urlParam) {
  88. $url = $originUrlPath.'&'.$urlParam.$urlParamSuffix;
  89. } else {
  90. $url = $originUrlPath.$urlParamSuffix;
  91. }
  92. $this->setRequestParam($originUrlPath);
  93. } else {
  94. if ($urlParam) {
  95. $url = $originUrlPath.'?'.$urlParam.$urlParamSuffix;
  96. } else {
  97. $url = $originUrlPath.$urlParamSuffix;
  98. }
  99. }
  100. return $baseUrl.$url;
  101. } else {
  102. return $requestUri;
  103. }
  104. }
  105. /**
  106. * after get urlPath from db, if urlPath has get param ,
  107. * set the param to $_GET.
  108. */
  109. public function setRequestParam($originUrlPath)
  110. {
  111. $arr = explode('?', $originUrlPath);
  112. $yiiUrlParam = $arr[1];
  113. $arr = explode('&', $yiiUrlParam);
  114. foreach ($arr as $a) {
  115. list($key, $val) = explode('=', $a);
  116. $_GET[$key] = $val;
  117. }
  118. }
  119. /**
  120. * mongodb url_rewrite collection columns: _id, type ,custom_url, yii_url,
  121. * if selete date from $this->_urlRewriteModel, return the yii url.
  122. */
  123. protected function getOriginUrl($urlKey)
  124. {
  125. $UrlData = $this->_urlRewriteModel->find()->where([
  126. 'custom_url_key' => $urlKey,
  127. ])->asArray()->one();
  128. if ($UrlData['custom_url_key']) {
  129. return $UrlData['origin_url'];
  130. }
  131. }
  132. }