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')); }