From a946a863861f42f7f13c2a334015152acb24bbf9 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Mon, 11 Feb 2013 08:49:44 -0500 Subject: [PATCH] fixed parseUrl. --- framework/web/UrlRule.php | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/framework/web/UrlRule.php b/framework/web/UrlRule.php index 457b735..247633e 100644 --- a/framework/web/UrlRule.php +++ b/framework/web/UrlRule.php @@ -77,9 +77,9 @@ class UrlRule extends Object $length = strlen($match[0][0]); $offset = $match[0][1]; if ($this->pattern[$offset - 1] === '/' && $this->pattern[$offset + $length] === '/') { - $tr["<$name>"] = "(?P<$name>(?:/$pattern)?)"; + $tr["/<$name>"] = "(/(?P<$name>$pattern))?"; } else { - $tr["<$name>"] = "(?P<$name>(?:$pattern)?)"; + $tr["<$name>"] = "(?P<$name>$pattern)?"; } } else { $tr["<$name>"] = "(?P<$name>$pattern)"; @@ -93,7 +93,7 @@ class UrlRule extends Object } $this->template = preg_replace('/<(\w+):?([^>]+)?>/', '<$1>', $this->pattern); - $this->pattern = '#^' . strtr($this->template, $tr) . '$#u'; + $this->pattern = '#^' . trim(strtr($this->template, $tr), '/') . '$#u'; if ($this->routeParams !== array()) { $this->routeRule = '#^' . strtr($this->route, $tr2) . '$#u'; @@ -102,7 +102,26 @@ class UrlRule extends Object public function parseUrl($pathInfo) { - + if (!preg_match($this->pattern, $pathInfo, $matches)) { + return false; + } + $params = $this->defaults; + $tr = array(); + foreach ($matches as $name => $value) { + if ($value !== '') { + if (isset($this->routeParams[$name])) { + $tr[$this->routeParams[$name]] = $value; + } elseif (isset($this->paramRules[$name])) { + $params[$name] = $value; + } + } + } + if ($this->routeRule !== null) { + $route = strtr($this->route, $tr); + } else { + $route = $this->route; + } + return array($route, $params); } public function createUrl($route, $params) @@ -115,7 +134,6 @@ class UrlRule extends Object foreach ($this->routeParams as $name => $token) { if (isset($this->defaults[$name]) && strcmp($this->defaults[$name], $matches[$name]) === 0) { $tr[$token] = ''; - $tr["/$token/"] = '/'; } else { $tr[$token] = $matches[$name]; } @@ -137,7 +155,6 @@ class UrlRule extends Object unset($params[$name]); if (isset($this->paramRules[$name])) { $tr["<$name>"] = ''; - $tr["/<$name>/"] = '/'; } } elseif (!isset($this->paramRules[$name])) { return false; @@ -163,11 +180,4 @@ class UrlRule extends Object } return $url; } - - public function parse($pathInfo) - { - $route = ''; - $params = array(); - return array($route, $params); - } }