Yii應用程式的入口腳本最後一句啟動了WebApplication
Yii::createWebApplication($config)->run();
CApplication:
public function run() { $this->onBeginRequest(new CEvent($this)); $this->processRequest(); $this->onEndRequest(new CEvent($this)); }
processRequest()開始處理請求,由CWebApplicationreurl元件的parseUrl() 建立了$route (形式為controllerID/actionID的字串),runController()建立Controller物件開始處理http請求。
$route 的值可能存在以下幾種情況:
- 為空: 用defaultController 值代替;- “moduleID/controllerID/actionID”: module下的
- “controllerID/actionID” : 最常見的形式
- 「folder1/folder2/controllerID/actionID」 多層目錄下的控制器
runController首先呼叫createController()建立控制器物件
public function processRequest() { if(is_array($this->catchAllRequest) && isset($this->catchAllRequest[0])) { $route=$this->catchAllRequest[0]; foreach(array_splice($this->catchAllRequest,1) as $name=>$value) $_GET[$name]=$value; } else $route=$this->getUrlManager()->parseUrl($this->getRequest()); $this->runController($route); }
createController() 傳回一個已建立好的控制器物件和actionID, runController()呼叫控制器的init()方法和run($actionID)來運作控制器:
public function createController($route,$owner=null) { // $owner为空则设置为$this,即 $_app对象 if($owner===null) $owner=$this; // $route为空设置为defaultController,在$config里配置 if(($route=trim($route,’/'))===”) $route=$owner->defaultController; $caseSensitive=$this->getUrlManager()->caseSensitive; $route.=’/'; // 逐一取出 $route 按 ‘/’分割后的第一段进行处理 while(($pos=strpos($route,’/'))!==false) { // $id 里存放的是 $route 第一个 ‘/’前的部分 $id=substr($route,0,$pos); if(!preg_match(‘/^\w+$/’,$id)) return null; if(!$caseSensitive) $id=strtolower($id); // $route 存放’/’后面部分 $route=(string)substr($route,$pos+1); if(!isset($basePath)) // 完整$route的第一段 { // 如果$id在controllerMap[]里做了映射 // 直接根据$id创建controller对象 if(isset($owner->controllerMap[$id])) { return array( Yii::createComponent($owner->controllerMap[$id],$id,$owner===$this?null:$owner), $this->parseActionParams($route), ); } // $id 是系统已定义的 module,根据$id取得module对象作为$owner参数来createController if(($module=$owner->getModule($id))!==null) return $this->createController($route,$module); // 控制器所在的目录 $basePath=$owner->getControllerPath(); $controllerID=”; } else $controllerID.=’/'; $className=ucfirst($id).’Controller’; $classFile=$basePath.DIRECTORY_SEPARATOR.$className.’.php’; // 控制器类文件存在,则require并创建控制器对象&返回 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), ); } return null; } // 未找到控制器类文件,可能是多级目录,继续往子目录搜索 $controllerID.=$id; $basePath.=DIRECTORY_SEPARATOR.$id; } }
$controller->init()裡沒有動作, run():
public function runController($route) { if(($ca=$this->createController($route))!==null) { list($controller,$actionID)=$ca; $oldController=$this->_controller; $this->_controller=$controller; $controller->init(); $controller->run($actionID); $this->_controller=$oldController; } else throw new CHttpException( 404, Yii::t(‘yii’,'Unable to resolve the request “{route}”.’, array( ‘{route}’=>$route===” ? $this->defaultController:$route))); }
$controller->run($actionID)裡先建立了Action物件:
public function run($actionID) { if(($action=$this->createAction($actionID))!==null) { if(($parent=$this->getModule())===null) $parent=Yii::app(); if($parent->beforeControllerAction($this,$action)) { $this->runActionWithFilters($action,$this->filters()); $parent->afterControllerAction($this,$action); } } else $this->missingAction($actionID); }
這裡可以看到控制器並不是直接呼叫了action方法,而是需要一個Action物件來運行控制器動作,這樣就統一了控制器方法和actions映射的action物件對action的處理,即兩種形式的action處理都統一為IAction介面的run()呼叫。
IAction介面要求實作run(),getId(),getController () 三個方法,Yii提供的CAction類別要求建構函式提供Controller和Id並實作了getId()和getController ()的處理,Action類別從CAction繼承即可。
CInlineAction在web/action下,run()是很簡單的處理過程,呼叫了Controller的action方法:
public function createAction($actionID) { // 为空设置为defaultAction if($actionID===”) $actionID=$this->defaultAction; // 控制器里存在 ‘action’.$actionID 的方法,创建CInlineAction对象 if(method_exists($this,’action’.$actionID) && strcasecmp($actionID,’s')) // we have actions method return new CInlineAction($this,$actionID); // 否则根据actions映射来创建Action对象 else return $this->createActionFromMap($this->actions(),$actionID,$actionID); }
回到$controller->run($actionID)
class CInlineAction extends CAction { public function run() { $method=’action’.$this->getId(); $this->getController()->$method(); } }
Yii::app()- >beforeControllerAction() 實際上是固定回傳true的,所以action物件實際上是透過控制器的runActionWithFilters()被run的
public function run($actionID) { if(($action=$this->createAction($actionID))!==null) { if(($parent=$this->getModule())===null) $parent=Yii::app(); if($parent->beforeControllerAction($this,$action)) { $this->runActionWithFilters($action,$this->filters()); $parent->afterControllerAction($this,$action); } } else $this->missingAction($actionID); }
沒有篩選器,runAction()就是最終要呼叫前面建立的action物件的run()方法:
public function runActionWithFilters($action,$filters) { // 控制器里没有设置过滤器 if(empty($filters)) $this->runAction($action); else { // 创建过滤器链对象并运行 $priorAction=$this->_action; $this->_action=$action; CFilterChain::create($this,$action,$filters)->run(); $this->_action=$priorAction; } }
每個filter都要實現IFilter接口,filter實現的preFilter()方法在$action->run()之前調用,如果判斷action可以執行則返回true,否則返回false
if($filter1- >preFilter())
if($filter2->preFilter())if($filtern->preFilter())
$action->run()
$filtern->postFilter()
$filter2->postFilter($filtern->postFilter()
$filter2->postFilter( )
$filter1->postFilter()
public function runAction($action) { $priorAction=$this->_action; $this->_action=$action; if($this->beforeAction($action)) { $action->run(); $this->afterAction($action); } $this->_action=$priorAction; }
$view 以'/'開頭的,以系統views目錄作為起始目錄+$view+.php
$view含有別名的,找出別名的真實路徑
其他的以modele view目錄作為起始目錄+$view+.php
public function renderPartial($view,$data=null,$return=false,$processOutput=false) { if(($viewFile=$this->getViewFile($view))!==false) { $output=$this->renderFile($viewFile,$data,true); if($processOutput) $output=$this->processOutput($output); if($return) return $output; else echo $output; } else throw new CException(Yii::t(‘yii’,'{controller} cannot find the requested view “{view}”.’, array(‘{controller}’=>get_class($this), ‘{view}’=>$view))); }
public function renderFile($viewFile,$data=null,$return=false) { $widgetCount=count($this->_widgetStack); // 如果配置了其他的ViewRenderer if(($renderer=Yii::app()->getViewRenderer())!==null) $content=$renderer->renderFile($this,$viewFile,$data,$return); else // yii 自身的render $content=$this->renderInternal($viewFile,$data,$return); if(count($this->_widgetStack)===$widgetCount) return $content; else { $widget=end($this->_widgetStack); throw new CException(Yii::t(‘yii’,'{controller} contains improperly nested widget tags in its view “{view}”. A {widget} widget does not have an endWidget() call.’,array(‘{controller}’=>get_class($this), ‘{view}’=>$viewFile, ‘{widget}’=>get_class($widget)))); } }
public function renderInternal($_viewFile_,$_data_=null,$_return_=false) { // extract函数将$_data_从数组中将变量导入到当前的符号表 if(is_array($_data_)) extract($_data_,EXTR_PREFIX_SAME,’data’); else $data=$_data_; if($_return_) { ob_start(); ob_implicit_flush(false); require($_viewFile_); return ob_get_clean(); } else require($_viewFile_); }
public function render($view,$data=null,$return=false) { $output=$this->renderPartial($view,$data,true); if(($layoutFile=$this->getLayoutFile($this->layout))!==false) $output=$this->renderFile($layoutFile,array(‘content’=>$output),true); $output=$this->processOutput($output); if($return) return $output; else echo $output; }