/** * 验证路由表规则 * @param Void * @return Void */ public function matchs($uri) { $matchs = array(); if ($this->routes) { if ($uri == '') { //uri为空,则使用默认路由规则(路由规则表最后一项) return $this->parse(NULL, NULL); } else { //遍历路由表规则,如果匹配其中一项,则退出 foreach ($this->routes as $rule) { //定义正则捕获组名 如:(<action>)-(<category>)=>(?<action>)-(?<category>) $pattern = preg_replace('/(?<=[(])(?=[<])/', '?', $rule[0]); //定义正则表达式字符范围 如:(?<action>)-(?<category>) => (?<action>[\w]+)-(?<category>[\w]+) $pattern = preg_replace('/(?<=[>])(?=[)])/', '[\\w]+', $pattern); if ($rule[1] && is_array($rule[1])) { //自定义字符范围 foreach ($rule[1] as $k => $v) { $pattern = preg_replace('/(?<=(' . $k . ')[>])\\[\\\\w\\]\\+(?=[)])/', $v, $pattern); } } //判断$uri是包含多字节,如果包含,则需要转码 if (!Utf8::isAscii($uri)) { $uri = iconv('gb2312', 'utf-8', $uri); } //将当前路由规格与uri进行匹配 if (preg_match("~^{$pattern}\$~u", $uri, $matchs)) { //成功匹配,交由Request处理 $default = array(); //默认路由规则 if ($rule[2]) { $default = $rule[2]; } return $this->parse($matchs, $default); break; } } } } //所有路由规则匹配失败,抛出异常 if (!$matchs) { throw new Ada_Exception('Unable to find a route to match'); } }