使用yii架構的url路徑一般形如hostname/?r=xxxx/xxxx/xxxx&sdfs=dsfdsf
我們可以看到有時會使用protected目錄下的controller,有時會使用module中controller,具體是如何處理的呢,請看如下的分析:
以下代碼摘自yii架構核心代碼%Yiiroot%/framework/web/CWebApplication.php
複製代碼 代碼如下:
=================================================================================================
//1.runController是執行一個controller的方法,$route是$_GET['r']
public function runController($route)
{
//在這裡調用createController先去建立一個controller執行個體,由此可見createController是選擇controller的關鍵
if(($ca=$this->createController($route))!==null)
{
list($controller,$actionID)=$ca;
$oldC
$this->_c
$controller->init();
$controller->run($actionID);
$this->_c
}
else
throw new CHttpException(404,Yii::t('yii','Unable to resolve the request "{route}".',
array('{route}'=>$route===''?$this->defaultController:$route)));
}
==================================================================================================
//2.接下來我們分析createController,假設我們訪問的route是site/contact
public function createController($route,$owner=null)
{
//首次進入這個函數,$owner參數為空白
if($owner===null)
$owner=$this;
//如果$route參數中不含/,那麼使用預設的controller
if(($route=trim($route,'/'))==='')
$route=$owner->defaultController;
$caseSensitive=$this->getUrlManager()->caseSensitive;
//為了能夠完整運行下面的迴圈,給$route後面加一個/
$route.='/';
//將/的位置儲存在$pos中
while(($pos=strpos($route,'/'))!==false)
{
//$id是前半部分,即site
$id=substr($route,0,$pos);
if(!preg_match('/^\w+$/',$id))
return null;
if(!$caseSensitive)
$id=strtolower($id);
//$route變成後半部分,即contact
$route=(string)substr($route,$pos+1);
//controller根目錄或子目錄首碼
if(!isset($basePath)) // first segment
{
//首次進入,$owner為空白,沒有這個成員變數
//非首次進入或$owner有值,有可能設定了這個成員變數,參見CWebModule類
if(isset($owner->controllerMap[$id]))
{
return array(
Yii::createComponent($owner->controllerMap[$id],$id,$owner===$this?null:$owner),
$this->parseActionParams($route),
);
}
//如果能通過getModule方法擷取到一個獨立模組,則再次調用createController,適用於site是module名的情況,參考protected/config/main.php設定檔,例如你的controller在%webroot%/protected/module/site/controller/ContactController.php
if(($module=$owner->getModule($id))!==null)
return $this->createController($route,$module);
//controller的目錄:
//對於CWebApplication,對應config['basePath'](參見設定檔)./controller/,例如你的controller在%webroot%/protected/controller/SiteController.php
//對於CModule的子類,對應改子類所在檔案夾./contoller/,例如你的controller在%webroot%/protected/module/site/controller/ContactController.php
$basePath=$owner->getControllerPath();
$c/';
$className=ucfirst($id).'Controller';
$classFile=$basePath.DIRECTORY_SEPARATOR.$className.'.php';
//如果$classFile存在,根據上面所得到的controller類檔案路徑,建立類執行個體
//如果不存在,則是子目錄下的controller,繼續迴圈尋找最終的controller,例如你的controller在%webroot%/protected/controller/somedir/SiteController
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;
}
}
以上就介紹了controler yii架構源碼分析之建立controller代碼,包括了controler方面的內容,希望對PHP教程有興趣的朋友有所協助。