From 626d0a26746c6f4186d02d8186592bdcf1d87327 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 20 May 2016 14:11:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BRequest=E5=92=8CUrl=E7=B1=BB?= =?UTF-8?q?=20=E5=8E=BB=E6=8E=89base=5Furl=E9=85=8D=E7=BD=AE=E5=8F=82?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- convention.php | 4 +-- library/think/Request.php | 73 ++++++++++++++++++++++++--------------- library/think/Url.php | 2 +- 3 files changed, 47 insertions(+), 32 deletions(-) diff --git a/convention.php b/convention.php index 55f2d50b..353fbc3a 100644 --- a/convention.php +++ b/convention.php @@ -63,8 +63,6 @@ return [ 'pathinfo_depr' => '/', // 获取当前页面地址的系统变量 默认为REQUEST_URI 'url_request_uri' => 'REQUEST_URI', - // 基础URL路径 - 'base_url' => $_SERVER["SCRIPT_NAME"], // URL伪静态后缀 'url_html_suffix' => '.html', // URL普通方式参数 用于自动生成 @@ -211,7 +209,7 @@ return [ 'paginate' => [ 'type' => 'bootstrap', 'var_page' => 'page', - 'list_rows' => 15 + 'list_rows' => 15, ], ]; diff --git a/library/think/Request.php b/library/think/Request.php index 8f1b6d2b..e7c29c72 100644 --- a/library/think/Request.php +++ b/library/think/Request.php @@ -21,6 +21,11 @@ class Request */ protected static $instance; + /** + * @var string 域名 + */ + protected $domain; + /** * @var string URL地址 */ @@ -32,9 +37,9 @@ class Request protected $baseUrl; /** - * @var string 基础路径 + * @var string 当前执行的文件 */ - protected $basePath; + protected $baseFile; /** * @var string 根目录 @@ -183,24 +188,39 @@ class Request return new self($options); } + /** + * 获取当前包含协议的域名 + * @access public + * @param string $url URL地址 + * @return string + */ + public function domain($domain = '') + { + if (!empty($domain)) { + $this->domain = $domain; + return; + } elseif (!$this->domain) { + $this->domain = $this->scheme() . '://' . $this->host(); + } + return $this->domain; + } + /** * 获取当前完整URL 包括QUERY_STRING * @access public * @param string $url URL地址 + * @param bool $domain 是否需要域名 * @return string */ public function url($url = '') { if (!empty($url)) { $this->url = $url; - } elseif ($this->url) { - return $this->url; - } else { - $url = $this->scheme() . '://' . $this->host(); - $url .= $_SERVER[Config::get('url_request_uri')]; - $this->url = $url; - return $url; + return; + } elseif (!$this->url) { + $this->url = $_SERVER[Config::get('url_request_uri')]; } + return true === $url ? $this->domain() . $this->url : $this->url; } /** @@ -213,27 +233,25 @@ class Request { if (!empty($url)) { $this->baseUrl = $url; - } elseif ($this->baseUrl) { - return $this->baseUrl; - } else { + return; + } elseif (!$this->baseUrl) { $this->baseUrl = rtrim($this->url(), '?' . $this->query()); - return $this->baseUrl; } + return true === $url ? $this->domain() . $this->baseUrl : $this->baseUrl; } /** - * 获取URL基础路径 SCRIPT_NAME + * 获取当前执行的文件 SCRIPT_NAME * @access public - * @param string $url URL地址 + * @param string $file 当前执行的文件 * @return string */ - public function basePath($url = '') + public function baseFile($file = '') { - if (!empty($url)) { - $this->basePath = $url; - } elseif ($this->basePath) { - return $this->basePath; - } else { + if (!empty($file)) { + $this->baseFile = $file; + return; + } elseif (!$this->baseFile) { $script_name = basename($_SERVER['SCRIPT_FILENAME']); if (basename($_SERVER['SCRIPT_NAME']) === $script_name) { $url = $_SERVER['SCRIPT_NAME']; @@ -246,9 +264,9 @@ class Request } elseif (isset($_SERVER['DOCUMENT_ROOT']) && strpos($_SERVER['SCRIPT_FILENAME'], $_SERVER['DOCUMENT_ROOT']) === 0) { $url = str_replace('\\', '/', str_replace($_SERVER['DOCUMENT_ROOT'], '', $_SERVER['SCRIPT_FILENAME'])); } - $this->basePath = $this->scheme() . '://' . $this->host() . $url; - return $this->basePath; + $this->baseFile = $url; } + return true === $file ? $this->domain() . $this->baseFile : $this->baseFile; } /** @@ -261,12 +279,11 @@ class Request { if (!empty($url)) { $this->root = $url; - } elseif ($this->root) { - return $this->root; - } else { - $this->root = rtrim(str_replace('\\', '/', dirname($this->basePath())), '/'); - return $this->root; + return; + } elseif (!$this->root) { + $this->root = rtrim(str_replace('\\', '/', dirname($this->baseFile())), '/'); } + return true === $url ? $this->domain() . $this->root : $this->root; } /** diff --git a/library/think/Url.php b/library/think/Url.php index 2987dc44..bd895c25 100644 --- a/library/think/Url.php +++ b/library/think/Url.php @@ -107,7 +107,7 @@ class Url // 检测域名 $domain = self::parseDomain($url, $domain); // URL组装 - $url = $domain . Config::get('base_url') . '/' . ltrim($url, '/'); + $url = $domain . Request::instance()->root() . '/' . ltrim($url, '/'); return $url; }