YII框架分析筆記9:url路由

以創建url路由爲例,從CWebApplication執行請求過程說起,如果在配置中設置了catchAllRequest,所有請求將會定位到配置中的路由中,否則的需要CUrlManager的parseUrl()方法解析解析url獲取路由。
/**
 * 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社區中兩幅圖分析的很詳細



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章