Skip to content

Commit

Permalink
改进Url类对可选参数的支持
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Mar 27, 2016
1 parent b26b793 commit 78142b7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
17 changes: 5 additions & 12 deletions library/think/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,19 +215,12 @@ public static function getRouteUrl($alias, &$vars = [])
}
// 检查变量匹配
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;
Expand Down
3 changes: 2 additions & 1 deletion tests/thinkphp/library/think/urlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public function testBuildModule()
$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-<name><id>', 'index/say');
Route::get('hello-<name><id?>', 'index/say');
$this->assertEquals('/hello-thinkphp', Url::build('index/say?name=thinkphp'));
$this->assertEquals('/hello-thinkphp2016', Url::build('index/say?name=thinkphp&id=2016'));
}

Expand Down

0 comments on commit 78142b7

Please sign in to comment.