Browse Source

fixed parseUrl.

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
a946a86386
  1. 36
      framework/web/UrlRule.php

36
framework/web/UrlRule.php

@ -77,9 +77,9 @@ class UrlRule extends Object
$length = strlen($match[0][0]); $length = strlen($match[0][0]);
$offset = $match[0][1]; $offset = $match[0][1];
if ($this->pattern[$offset - 1] === '/' && $this->pattern[$offset + $length] === '/') { if ($this->pattern[$offset - 1] === '/' && $this->pattern[$offset + $length] === '/') {
$tr["<$name>"] = "(?P<$name>(?:/$pattern)?)"; $tr["/<$name>"] = "(/(?P<$name>$pattern))?";
} else { } else {
$tr["<$name>"] = "(?P<$name>(?:$pattern)?)"; $tr["<$name>"] = "(?P<$name>$pattern)?";
} }
} else { } else {
$tr["<$name>"] = "(?P<$name>$pattern)"; $tr["<$name>"] = "(?P<$name>$pattern)";
@ -93,7 +93,7 @@ class UrlRule extends Object
} }
$this->template = preg_replace('/<(\w+):?([^>]+)?>/', '<$1>', $this->pattern); $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()) { if ($this->routeParams !== array()) {
$this->routeRule = '#^' . strtr($this->route, $tr2) . '$#u'; $this->routeRule = '#^' . strtr($this->route, $tr2) . '$#u';
@ -102,7 +102,26 @@ class UrlRule extends Object
public function parseUrl($pathInfo) 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) public function createUrl($route, $params)
@ -115,7 +134,6 @@ class UrlRule extends Object
foreach ($this->routeParams as $name => $token) { foreach ($this->routeParams as $name => $token) {
if (isset($this->defaults[$name]) && strcmp($this->defaults[$name], $matches[$name]) === 0) { if (isset($this->defaults[$name]) && strcmp($this->defaults[$name], $matches[$name]) === 0) {
$tr[$token] = ''; $tr[$token] = '';
$tr["/$token/"] = '/';
} else { } else {
$tr[$token] = $matches[$name]; $tr[$token] = $matches[$name];
} }
@ -137,7 +155,6 @@ class UrlRule extends Object
unset($params[$name]); unset($params[$name]);
if (isset($this->paramRules[$name])) { if (isset($this->paramRules[$name])) {
$tr["<$name>"] = ''; $tr["<$name>"] = '';
$tr["/<$name>/"] = '/';
} }
} elseif (!isset($this->paramRules[$name])) { } elseif (!isset($this->paramRules[$name])) {
return false; return false;
@ -163,11 +180,4 @@ class UrlRule extends Object
} }
return $url; return $url;
} }
public function parse($pathInfo)
{
$route = '';
$params = array();
return array($route, $params);
}
} }

Loading…
Cancel
Save