標籤:
以建立url路由為例,從CWebApplication執行請求過程說起,如果在配置中設定了catchAllRequest,所有請求將會定位到配置中的路由中,否則的需要CUrlManager的parseUrl()方法解析解析url擷取路由。
[php] view plaincopy
- /**
- * Parses the user request.
- * @param CHttpRequest $request the request application component
- * @return string the route (controllerID/actionID) and perhaps GET parameters in path format.
- */
- public function parseUrl($request)
- {
- if($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=Yii::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,Yii::t(‘yii‘,‘Unable to resolve the request "{route}".‘,
- array(‘{route}‘=>$pathInfo)));
- else
- return $pathInfo;
- }
- else if(isset($_GET[$this->routeVar]))
- return $_GET[$this->routeVar];
- else if(isset($_POST[$this->routeVar]))
- return $_POST[$this->routeVar];
- else
- return ‘‘;
- }
CUrlManager初始化的時候如果url格式(預設是get格式)如果是path格式,則通過配置中的rule數組建立路由規則對象,根據路由規則擷取內部路由,當路由都不匹配的時候會根據設定的useStrictParsing參數決定拋出一個404錯誤合適返回$pathinfo。如果不是path格式的話,會通過$_GET或者$_POST返回r後面的參數作為路由。
關與path路由的擷取和建立,YII社區中兩幅圖分析的很詳細
YII架構分析筆記9:url路由