From 004b87a47b02104b5f26e2a9d2879f4ddb67521c Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 25 Mar 2016 22:42:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BUrl=E7=B1=BB=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=B7=B7=E5=90=88=E5=8F=98=E9=87=8F=E8=B7=AF=E7=94=B1?= =?UTF-8?q?=E8=A7=84=E5=88=99=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Url.php | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/library/think/Url.php b/library/think/Url.php index bb0b8d5d..669f36f7 100644 --- a/library/think/Url.php +++ b/library/think/Url.php @@ -222,6 +222,12 @@ class Url } 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]); } } return $url; @@ -286,6 +292,18 @@ class Url $var = []; foreach (explode('/', $rule) as $val) { $optional = false; + if (false !== strpos($val, '<') && preg_match_all('/<(\w+(\??))>/', $val, $matches)) { + foreach ($matches[1] as $name) { + if (strpos($name, '?')) { + $name = substr($name, 0, -1); + $optional = true; + } else { + $optional = false; + } + $var[$name] = $optional ? 2 : 1; + } + } + if (0 === strpos($val, '[:')) { // 可选参数 $optional = true; @@ -294,8 +312,7 @@ class Url if (0 === strpos($val, ':')) { // URL变量 $name = substr($val, 1); - $type = $optional ? 2 : 1; - $var[$name] = $type; + $var[$name] = $optional ? 2 : 1; } } return $var;