From 78142b7bc2cb4c383ba9037ebb594ce142be6a22 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sun, 27 Mar 2016 11:20:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BUrl=E7=B1=BB=E5=AF=B9?= =?UTF-8?q?=E5=8F=AF=E9=80=89=E5=8F=82=E6=95=B0=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Url.php | 17 +++++------------ tests/thinkphp/library/think/urlTest.php | 3 ++- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/library/think/Url.php b/library/think/Url.php index 669f36f7..7c1945e4 100644 --- a/library/think/Url.php +++ b/library/think/Url.php @@ -215,19 +215,12 @@ class Url } // 检查变量匹配 if (self::pattern($pattern, $vars)) { - foreach ($vars as $key => $val) { - if (false !== strpos($url, '[:' . $key . ']')) { - $url = str_replace('[:' . $key . ']', $val, $url); - unset($vars[$key]); - } elseif (false !== strpos($url, ':' . $key)) { - $url = str_replace(':' . $key, $val, $url); - unset($vars[$key]); - } elseif (false !== strpos($url, '<' . $key . '>')) { - $url = str_replace('<' . $key . '>', $val, $url); - unset($vars[$key]); - } elseif (false !== strpos($url, '<' . $key . '?>')) { - $url = str_replace('<' . $key . '?>', $val, $url); + foreach ($pattern as $key => $val) { + if (isset($vars[$key])) { + $url = str_replace(['[:' . $key . ']', '<' . $key . '?>', ':' . $key . '', '<' . $key . '>'], $vars[$key], $url); unset($vars[$key]); + } else { + $url = str_replace(['[:' . $key . ']', '<' . $key . '?>'], '', $url); } } return $url; diff --git a/tests/thinkphp/library/think/urlTest.php b/tests/thinkphp/library/think/urlTest.php index bf98caf0..8150f1ab 100644 --- a/tests/thinkphp/library/think/urlTest.php +++ b/tests/thinkphp/library/think/urlTest.php @@ -34,7 +34,8 @@ class urlTest extends \PHPUnit_Framework_TestCase $this->assertEquals('/hello/10', Url::build('index/hello?id=10')); $this->assertEquals('/hello/10.html', Url::build('index/hello', 'id=10', 'html')); - Route::get('hello-', 'index/say'); + Route::get('hello-', 'index/say'); + $this->assertEquals('/hello-thinkphp', Url::build('index/say?name=thinkphp')); $this->assertEquals('/hello-thinkphp2016', Url::build('index/say?name=thinkphp&id=2016')); }