mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 15:12:48 +08:00
改进Url类对可选参数的支持
This commit is contained in:
@@ -215,19 +215,12 @@ class Url
|
|||||||
}
|
}
|
||||||
// 检查变量匹配
|
// 检查变量匹配
|
||||||
if (self::pattern($pattern, $vars)) {
|
if (self::pattern($pattern, $vars)) {
|
||||||
foreach ($vars as $key => $val) {
|
foreach ($pattern as $key => $val) {
|
||||||
if (false !== strpos($url, '[:' . $key . ']')) {
|
if (isset($vars[$key])) {
|
||||||
$url = str_replace('[:' . $key . ']', $val, $url);
|
$url = str_replace(['[:' . $key . ']', '<' . $key . '?>', ':' . $key . '', '<' . $key . '>'], $vars[$key], $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);
|
|
||||||
unset($vars[$key]);
|
unset($vars[$key]);
|
||||||
|
} else {
|
||||||
|
$url = str_replace(['[:' . $key . ']', '<' . $key . '?>'], '', $url);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $url;
|
return $url;
|
||||||
|
|||||||
@@ -34,7 +34,8 @@ class urlTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals('/hello/10', Url::build('index/hello?id=10'));
|
$this->assertEquals('/hello/10', Url::build('index/hello?id=10'));
|
||||||
$this->assertEquals('/hello/10.html', Url::build('index/hello', 'id=10', 'html'));
|
$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'));
|
$this->assertEquals('/hello-thinkphp2016', Url::build('index/say?name=thinkphp&id=2016'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user