Request.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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\yii\web;
  10. use Yii;
  11. use yii\base\InvalidConfigException;
  12. /**
  13. * rewrite class \yii\web\Request
  14. * use custom url in our system, example: www.example.com/xxxxx.html, this file is not
  15. * exit in our system, In order to consider SEO, we can use db storage map between custom url and yii url
  16. * when request visit /xxxx.html, select this custom url in mongodb, return the yii url ,ex. /product/index?_id=3
  17. * then , resolve /product/index?_id=3 .
  18. * @author Terry Zhao <2358269014@qq.com>
  19. * @since 1.0
  20. */
  21. class Request extends \yii\web\Request
  22. {
  23. /**
  24. * rewrite yii\web\Request resolveRequestUri().
  25. */
  26. protected function resolveRequestUri()
  27. {
  28. if (isset($_SERVER['HTTP_X_REWRITE_URL'])) { // IIS
  29. $requestUri = $_SERVER['HTTP_X_REWRITE_URL'];
  30. } elseif (isset($_SERVER['REQUEST_URI'])) {
  31. $requestUri = $_SERVER['REQUEST_URI'];
  32. if ($requestUri !== '' && $requestUri[0] !== '/') {
  33. $requestUri = preg_replace('/^(http|https):\/\/[^\/]+/i', '', $requestUri);
  34. }
  35. } elseif (isset($_SERVER['ORIG_PATH_INFO'])) { // IIS 5.0 CGI
  36. $requestUri = $_SERVER['ORIG_PATH_INFO'];
  37. if (!empty($_SERVER['QUERY_STRING'])) {
  38. $requestUri .= '?' . $_SERVER['QUERY_STRING'];
  39. }
  40. } else {
  41. throw new InvalidConfigException('Unable to determine the request URI.');
  42. }
  43. /*
  44. * Replace Code
  45. * //return $requestUri;
  46. * To:
  47. */
  48. return $this->getRewriteUri($requestUri);
  49. }
  50. /**
  51. * get module request url by db ;.
  52. */
  53. protected function getRewriteUri($requestUri)
  54. {
  55. $baseUrl = $this->getBaseUrl();
  56. $requestUriRelative = $requestUri;
  57. if ($baseUrl) {
  58. $requestUriRelative = substr($requestUriRelative, strlen($baseUrl));
  59. }
  60. $urlKey = '';
  61. $urlParam = '';
  62. $urlParamSuffix = '';
  63. // 从url中解析出来 urlKey部分,参数部分,已经#后面的部分
  64. if (strstr($requestUriRelative, '#')) {
  65. list($urlNoSuffix, $urlParamSuffix) = explode('#', $requestUriRelative);
  66. if (strstr($urlNoSuffix, '?')) {
  67. list($urlKey, $urlParam) = explode('?', $urlNoSuffix);
  68. }
  69. } elseif (strstr($requestUriRelative, '?')) {
  70. list($urlKey, $urlParam) = explode('?', $requestUriRelative);
  71. } else {
  72. $urlKey = $requestUriRelative;
  73. }
  74. if ($urlParamSuffix) {
  75. $urlParamSuffix = '#'.$urlParamSuffix;
  76. }
  77. // 通过上面解析的urlKey,传入到:Yii::$service->url->getOriginUrl($urlKey)
  78. // 得到原来的url key,这样,我们将自定义的url key,通过查询的方式,找到Yii2框架的url key
  79. if (Yii::$service->url->showScriptName) {
  80. $urlKey = str_replace('/index.php', '', $urlKey);
  81. $originUrlPath = Yii::$service->url->getOriginUrl($urlKey);
  82. } else {
  83. $originUrlPath = Yii::$service->url->getOriginUrl($urlKey);
  84. }
  85. //echo $urlKey;
  86. //echo $originUrlPath;
  87. //exit;
  88. if ($originUrlPath) {
  89. if (strstr($originUrlPath, '?')) {
  90. if ($urlParam) {
  91. $url = $originUrlPath.'&'.$urlParam.$urlParamSuffix;
  92. } else {
  93. $url = $originUrlPath.$urlParamSuffix;
  94. }
  95. $this->setRequestParam($originUrlPath);
  96. } else {
  97. if ($urlParam) {
  98. $url = $originUrlPath.'?'.$urlParam.$urlParamSuffix;
  99. } else {
  100. $url = $originUrlPath.$urlParamSuffix;
  101. }
  102. }
  103. return $baseUrl.$url;
  104. } else {
  105. return $requestUri;
  106. }
  107. }
  108. /**
  109. * after get urlPath from db, if urlPath has get param ,
  110. * set the param to $_GET.
  111. */
  112. public function setRequestParam($originUrlPath)
  113. {
  114. $arr = explode('?', $originUrlPath);
  115. $yiiUrlParam = $arr[1];
  116. $arr = explode('&', $yiiUrlParam);
  117. foreach ($arr as $a) {
  118. list($key, $val) = explode('=', $a);
  119. $_GET[$key] = $val;
  120. }
  121. }
  122. /*
  123. * mongodb url_rewrite collection columns: _id, type ,custom_url, yii_url,
  124. * if selete date from UrlRewrite, return the yii url.
  125. */
  126. /*
  127. protected function getOriginUrl($urlKey){
  128. $UrlData = UrlRewrite::find()->where([
  129. 'custom_url_key' => $urlKey,
  130. ])->asArray()->one();
  131. if($UrlData['custom_url_key']){
  132. return $UrlData['origin_url'];
  133. }
  134. return ;
  135. }
  136. */
  137. }