/** * 根据路由创建控制器 */ public function createController($route, $owner = null) { // 当前控制器触发应用 if ($owner === null) { $owner = $this; } // 获得默认控制器 if (($route = trim($route, '/')) === '') { $route = $owner->defaultController; } // 是否严格区分大小写 $caseSensitive = $this->getUrlManager()->caseSensitive; $route .= '/'; // 处理路由 while (($pos = strpos($route, '/')) !== false) { $id = substr($route, 0, $pos); if (!preg_match('/^\\w+$/', $id)) { return null; } if (!$caseSensitive) { $id = strtolower($id); } $route = (string) substr($route, $pos + 1); if (!isset($basePath)) { if (isset($owner->controllerMap[$id])) { return array(Nbt::createComponent($owner->controllerMap[$id], $id, $owner === $this ? null : $owner), $this->parseActionParams($route), $id); } $basePath = $owner->getControllerPath(); $controllerID = ''; } else { $controllerID .= '/'; } $className = ucfirst($id) . 'Controller'; $classFile = $basePath . DIRECTORY_SEPARATOR . $className . '.php'; if (is_file($classFile)) { if (!class_exists($className, false)) { require $classFile; } if (class_exists($className, false) && is_subclass_of($className, 'CController')) { $id[0] = strtolower($id[0]); return array(new $className($controllerID . $id, $owner === $this ? null : $owner), $this->parseActionParams($route), $id); } return null; } $controllerID .= $id; $basePath .= DIRECTORY_SEPARATOR . $id; } }
/** * 解析伪静态 URL * * @param CHttpRequest $request CHttpRequest 对象 * @return string */ public function parseUrl($request) { if (isset($_GET[$this->routeVar])) { return $_GET[$this->routeVar]; } elseif (isset($_POST[$this->routeVar])) { return $_POST[$this->routeVar]; } elseif ($this->getUrlFormat() === self::PATH_FORMAT) { $rawPathInfo = $request->getPathInfo(); $pathInfo = $this->removeUrlSuffix($rawPathInfo, $this->urlSuffix); foreach ($this->_rules as $i => $rule) { if (is_array($rule)) { $this->_rules[$i] = $rule = Nbt::createComponent($rule); } if (($r = $rule->parseUrl($this, $request, $pathInfo, $rawPathInfo)) !== false) { return isset($_GET[$this->routeVar]) ? $_GET[$this->routeVar] : $r; } } if ($this->useStrictParsing) { throw new CHttpException(404, "Unable to resolve the request '{$pathInfo}'."); } else { return $pathInfo; } } else { return ''; } }