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

以下代碼摘自yii框架 " /> www.日韩在线,成人网在线免费观看,久久久久久艹

四虎精品视频-四虎精品成人免费网站-四虎黄色网-四虎国产视频-国产免费91-国产蜜臀97一区二区三区

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

使用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í)例,由此可見(jiàn)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.接下來(lái)我們分析createController,假設(shè)我們?cè)L問(wèn)的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為空,沒(méi)有這個(gè)成員變量
//非首次進(jìn)入或$owner有值,有可能設(shè)置了這個(gè)成員變量,參見(jiàn)CWebModule類
if(isset($owner->controllerMap[$id]))
{
return array(
Yii::createComponent($owner->controllerMap[$id],$id,$owner===$this?null:$owner),
$this->parseActionParams($route),
);
}
//如果能通過(guò)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'](參見(jiàn)配置文件)./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;
}
} 

php技術(shù)yii框架源碼分析之創(chuàng)建controller代碼,轉(zhuǎn)載需保留來(lái)源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 新人类男友会触电电视剧免费观看全集 | 美女网站免费观看视频| 美女游泳| 儿童手绘中国地图| rima horton| 曹查理的十部经典电影| 日本电车系列| 孙东杓| 1到100数字表图片| 李道新| 无耻之徒阿曼达| 辐射避难所掉脑袋问题答案| 电车男| 只要有你还珠格格| 有风的地方| 香谱七十二图解| 我仍在此 电影| 好男人电视剧| 我的仨妈俩爸演员表| 狼来了ppt免费下载| 戴安·梅尔| 热带夜的引诱| 美丽的日子 电视剧| 硅胶娃娃实战视频| 进击的巨人2| 陈牧驰个人资料简介图片| 成年奶妈| 五的词语| 邓为个人资料及简历| 黄姓的研究报告| 王者图片| 温州新闻| 吸痰护理ppt课件| 坐月子喝什么汤最好最有营养| 未来少年柯南 动漫| 打电话简谱| 黄明昊身高| 温州新闻| 天与地越南版| 热巴电视剧在线观看免费| 数码宝贝第三部|