一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - PHP教程 - yii框架源碼分析之創(chuàng)建controller代碼

yii框架源碼分析之創(chuàng)建controller代碼

2019-12-02 13:59PHP教程網(wǎng) PHP教程

我們可以看到有時(shí)會(huì)使用protected目錄下的controller,有時(shí)會(huì)使用module中controller,具體是如何處理的呢,請(qǐng)看如下的分析

使用yii框架的url路徑一般形如hostname/?r=xxxx/xxxx/xxxx&sdfs=dsfdsf 

我們可以看到有時(shí)會(huì)使用protected目錄下的controller,有時(shí)會(huì)使用module中controller,具體是如何處理的呢,請(qǐng)看如下的分析: 

以下代碼摘自yii框架核心代碼%Yiiroot%/framework/web/CWebApplication.php 

復(fù)制代碼代碼如下:


================================================================================================= 
//1.runController是執(zhí)行一個(gè)controller的方法,$route是$_GET['r'] 
public function runController($route) 

//在這里調(diào)用createController先去創(chuàng)建一個(gè)controller實(shí)例,由此可見createController是選擇controller的關(guān)鍵 
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))); 

================================================================================================== 
//2.接下來我們分析createController,假設(shè)我們?cè)L問的route是site/contact 
public function createController($route,$owner=null) 

//首次進(jìn)入這個(gè)函數(shù),$owner參數(shù)為空 
if($owner===null) 
$owner=$this; 
//如果$route參數(shù)中不含/,那么使用默認(rèn)的controller 
if(($route=trim($route,'/'))==='') 
$route=$owner->defaultController; 
$caseSensitive=$this->getUrlManager()->caseSensitive; 
//為了能夠完整運(yùn)行下面的循環(huán),給$route后面加一個(gè)/ 
$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 

//首次進(jìn)入,$owner為空,沒有這個(gè)成員變量 
//非首次進(jìn)入或$owner有值,有可能設(shè)置了這個(gè)成員變量,參見CWebModule類 
if(isset($owner->controllerMap[$id])) 

return array( 
Yii::createComponent($owner->controllerMap[$id],$id,$owner===$this?null:$owner), 
$this->parseActionParams($route), 
); 

//如果能通過getModule方法獲取到一個(gè)獨(dú)立模塊,則再次調(diào)用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的目錄: 
//對(duì)于CWebApplication,對(duì)應(yīng)config['basePath'](參見配置文件)./controller/,例如你的controller在%webroot%/protected/controller/SiteController.php 
//對(duì)于CModule的子類,對(duì)應(yīng)改子類所在文件夾./contoller/,例如你的controller在%webroot%/protected/module/site/controller/ContactController.php 
$basePath=$owner->getControllerPath(); 
$controllerID=''; 

else 
$controllerID.='/'; 
$className=ucfirst($id).'Controller'; 
$classFile=$basePath.DIRECTORY_SEPARATOR.$className.'.php'; 
//如果$classFile存在,根據(jù)上面所得到的controller類文件路徑,創(chuàng)建類實(shí)例 
//如果不存在,則是子目錄下的controller,繼續(xù)循環(huán)尋找最終的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; 

}  

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲成年网站在线777 | 97精品国产高清在线看入口 | 皇上好大好硬好涨好深好爽 | 男女交性特一级 | 美女扒开屁股 | 亚洲国产精品无码中文字幕 | 羞羞视频动漫 | 办公室操秘书 | 午夜dj影院在线视频观看完整 | 亚洲另类第一页 | 成人精品区| 国产1广场舞丰满老女偷 | 国产精品99久久 | 精品久久99麻豆蜜桃666 | 521色香蕉网在线观看免费 | 故意短裙公车被强好爽在线播放 | 91精品国产色综合久久不卡蜜 | 成人综合网址 | 情侣宾馆愉拍自拍视频 | 精品网站 | 思思玖玖玖在线精品视频 | 国产亚洲一欧美一区二区三区 | 欧美精品一区二区在线观看播放 | 女人特黄大aaaaaa大片 | 天天干天天日天天射天天操毛片 | 麻豆网站视频国产在线观看 | 明星乱亚洲 | 国产高清在线精品一区 | 欧美在线看片a免费观看 | 亚洲视频在线观看不卡 | 91原创国产 | 精品免费久久久久久影院 | 火影忍者羞羞 | 欧美亚洲国产一区二区三区 | 青草视频网址 | 男人狂躁女人下面的视频免费 | 夫妻性生活在线 | 荡娃艳妇系列小说 | 男女车车好快的车车免费网站 | 国产主播精品在线 | 日本免费在线播放 |