改进Url类对可选参数的支持

This commit is contained in:
thinkphp
2016-03-27 11:20:12 +08:00
parent b26b7934a8
commit 78142b7bc2
2 changed files with 7 additions and 13 deletions

View File

@@ -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;

View File

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