我們可以看到有時會使用protected目錄下的controller,有時會使用module中controller,具體是如何處理的呢,請看如下的分析:

以下代碼摘自yii框架 " /> 99精品视频在线,乳欲性高清在线,国产成人免费观看

天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

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

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

我們可以看到有時會使用protected目錄下的controller,有時會使用module中controller,具體是如何處理的呢,請看如下的分析:

以下代碼摘自yii框架核心代碼%Yiiroot%/framework/web/CWebApplication.php
復制代碼 代碼如下:
=================================================================================================
//1.runController是執(zhí)行一個controller的方法,$route是$_GET['r']
public function runController($route)
{
//在這里調用createController先去創(chuàng)建一個controller實例,由此可見createController是選擇controller的關鍵
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,假設我們訪問的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;
//為了能夠完整運行下面的循環(huán),給$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();
$controllerID='';
}
else
$controllerID.='/';
$className=ucfirst($id).'Controller';
$classFile=$basePath.DIRECTORY_SEPARATOR.$className.'.php';
//如果$classFile存在,根據上面所得到的controller類文件路徑,創(chuàng)建類實例
//如果不存在,則是子目錄下的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;
}
} 

php技術yii框架源碼分析之創(chuàng)建controller代碼,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 日本精品久久久久中文字幕2 | 欧美性极品黑人hd | 做暖免费观看日本 | 出租屋交换人妻 全文 | 亚洲幼女网 | 天天躁人人躁人人躁狂躁 | 日韩精品 电影一区 亚洲高清 | 耻辱诊察室1一4集动漫在线观看 | 免费看大黄高清网站视频在线 | 色噜噜狠狠色综合欧洲 | 97视频视频人人碰视频 | 黄小说免费看 | 久草热8精品视频在线观看 久草青青在线 | 亚洲成人黄色片 | 啊灬啊别停灬用力啊老师 | 菠萝菠萝蜜在线观看视频 | 国产中文字幕乱码一区 | 国产精品悠悠久久人妻精品 | 亚洲AV无码乱码国产麻豆穿越 | qovd电影 | 狠狠干女人 | 最近的2019中文字幕HD | 99re6热这里在线精品视频 | 亚洲视频在线观看免费 | 男女生爽爽爽视频免费观看 | 国产在线精品视频资源 | 色欲AV精品人妻一二三区 | 6080yy亚洲久久无码 | 果冻传媒剧情在线观看 | 亚洲欧美日韩一级特黄在线 | H揉捏娇喘乳叫床NP调教视频 | 一本道色综合手机久久 | 豆奶视频在线高清观看 | 国产一区二区波多野结衣 | 粉嫩AV国产一区二区福利姬 | 又色又爽又黄gif动态视频 | 在线不卡日本v二区 | 国产亚洲精品久久7777777 | 麻豆AV久久无码精品九九 | 日本人添下面的全过程 | 亚洲精品国产一区二区贰佰信息网 |