From 7eed63c2ee5d1d8403e7baf352765c9966bceed0 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 19 May 2016 16:01:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BRequest=E7=B1=BB=E7=9A=84url?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E6=96=B9=E6=B3=95=20=EF=BC=88=E4=BB=8D?= =?UTF-8?q?=E9=9C=80=E5=AE=8C=E5=96=84=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Request.php | 71 ++++++++++++++++++++++++++++++++++----- 1 file changed, 63 insertions(+), 8 deletions(-) diff --git a/library/think/Request.php b/library/think/Request.php index ef0edbfc..1377cb0c 100644 --- a/library/think/Request.php +++ b/library/think/Request.php @@ -31,6 +31,11 @@ class Request */ protected $baseUrl; + /** + * @var string 基础路径 + */ + protected $basePath; + /** * @var string 根目录 */ @@ -179,7 +184,7 @@ class Request } /** - * 获取当前URL + * 获取当前完整URL 包括QUERY_STRING * @access public * @param string $url URL地址 * @return string @@ -188,23 +193,53 @@ class Request { if (!empty($url)) { $this->url = $url; + } elseif ($this->url) { + return $this->url; } else { - return $this->url ?: $_SERVER[Config::get('url_request_uri')]; + $url = $this->scheme() . '://' . $this->host(); + $url .= $_SERVER[Config::get('url_request_uri')]; + $this->url = $url; + return $url; } } /** - * 获取基础URL + * 获取当前URL 不含QUERY_STRING * @access public * @param string $url URL地址 * @return string */ - public function baseUrl($url = '') + public function baeUrl($url = '') { if (!empty($url)) { $this->baseUrl = $url; + } elseif ($this->baseUrl) { + return $this->baseUrl; } else { - return $this->baseUrl ?: rtrim($_SERVER['SCRIPT_NAME'], '/'); + $url = $this->scheme() . '://' . $this->host(); + $url .= $_SERVER['PHP_SELF']; + $this->baseUrl = $url; + return $url; + } + } + + /** + * 获取URL基础路径 SCRIPT_NAME + * @access public + * @param string $url URL地址 + * @return string + */ + public function basePath($url = '') + { + if (!empty($url)) { + $this->basePath = $url; + } elseif ($this->basePath) { + return $this->basePath; + } else { + $url = $this->scheme() . '://' . $this->host(); + $url .= rtrim($_SERVER['SCRIPT_NAME'], '/'); + $this->basePath = $url; + return $url; } } @@ -218,12 +253,12 @@ class Request { if (!empty($url)) { $this->root = $url; - } elseif ($this->root) { return $this->root; } else { - $_root = rtrim(dirname($this->baseUrl()), '/'); - return ('/' == $_root || '\\' == $_root) ? '' : $_root; + $_root = rtrim(dirname($this->baseUrl()), '/'); + $this->root = ('/' == $_root || '\\' == $_root) ? '' : $_root; + return $this->root; } } @@ -589,6 +624,26 @@ class Request return $_SERVER['SERVER_PORT']; } + /** + * 当前请求 SERVER_PROTOCOL + * @access public + * @return integer + */ + public function protocol() + { + return $_SERVER['SERVER_PROTOCOL']; + } + + /** + * 当前请求 REMOTE_PORT + * @access public + * @return integer + */ + public function remotePort() + { + return $_SERVER['REMOTE_PORT']; + } + /** * 获取当前请求的路由 * @access public