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

來源:互聯網
上載者:User

標籤:

以建立url路由為例,從CWebApplication執行請求過程說起,如果在配置中設定了catchAllRequest,所有請求將會定位到配置中的路由中,否則的需要CUrlManager的parseUrl()方法解析解析url擷取路由。

[php] view plaincopy
  1. /** 
  2.  * Parses the user request. 
  3.  * @param CHttpRequest $request the request application component 
  4.  * @return string the route (controllerID/actionID) and perhaps GET parameters in path format. 
  5.  */  
  6. public function parseUrl($request)  
  7. {  
  8.     if($this->getUrlFormat()===self::PATH_FORMAT)  
  9.     {  
  10.         $rawPathInfo=$request->getPathInfo();  
  11.         $pathInfo=$this->removeUrlSuffix($rawPathInfo,$this->urlSuffix);  
  12.         foreach($this->_rules as $i=>$rule)  
  13.         {  
  14.             if(is_array($rule))  
  15.                 $this->_rules[$i]=$rule=Yii::createComponent($rule);  
  16.             if(($r=$rule->parseUrl($this,$request,$pathInfo,$rawPathInfo))!==false)  
  17.                 return isset($_GET[$this->routeVar]) ? $_GET[$this->routeVar] : $r;  
  18.         }  
  19.         if($this->useStrictParsing)  
  20.             throw new CHttpException(404,Yii::t(‘yii‘,‘Unable to resolve the request "{route}".‘,  
  21.                 array(‘{route}‘=>$pathInfo)));  
  22.         else  
  23.             return $pathInfo;  
  24.     }  
  25.     else if(isset($_GET[$this->routeVar]))  
  26.         return $_GET[$this->routeVar];  
  27.     else if(isset($_POST[$this->routeVar]))  
  28.         return $_POST[$this->routeVar];  
  29.     else  
  30.         return ‘‘;  
  31. }  

CUrlManager初始化的時候如果url格式(預設是get格式)如果是path格式,則通過配置中的rule數組建立路由規則對象,根據路由規則擷取內部路由,當路由都不匹配的時候會根據設定的useStrictParsing參數決定拋出一個404錯誤合適返回$pathinfo。如果不是path格式的話,會通過$_GET或者$_POST返回r後面的參數作為路由。


關與path路由的擷取和建立,YII社區中兩幅圖分析的很詳細


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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.