yii框架源碼分析之創(chuàng)建controller代碼
更新時間:2011年06月28日 19:26:11 作者:
我們可以看到有時會使用protected目錄下的controller,有時會使用module中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)
{
//在這里調(diào)用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)
{
//首次進入這個函數(shù),$owner參數(shù)為空
if($owner===null)
$owner=$this;
//如果$route參數(shù)中不含/,那么使用默認的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方法獲取到一個獨立模塊,則再次調(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的目錄:
//對于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存在,根據(jù)上面所得到的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;
}
}
我們可以看到有時會使用protected目錄下的controller,有時會使用module中controller,具體是如何處理的呢,請看如下的分析:
以下代碼摘自yii框架核心代碼%Yiiroot%/framework/web/CWebApplication.php
復制代碼 代碼如下:
=================================================================================================
//1.runController是執(zhí)行一個controller的方法,$route是$_GET['r']
public function runController($route)
{
//在這里調(diào)用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)
{
//首次進入這個函數(shù),$owner參數(shù)為空
if($owner===null)
$owner=$this;
//如果$route參數(shù)中不含/,那么使用默認的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方法獲取到一個獨立模塊,則再次調(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的目錄:
//對于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存在,根據(jù)上面所得到的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 雜談《重構(gòu)-改善既有代碼的設計》之三 重新組織數(shù)據(jù)
承接上文的PHP 雜談《重構(gòu)-改善既有代碼的設計》之 重新組織你的函數(shù)繼續(xù)重構(gòu)方面的內(nèi)容2012-04-04thinkphp在低版本Nginx 下支持PATHINFO的方法分享
本文給大家分享的是如何讓thinkPHP在低版本的Nginx下支持PATHINFO去掉index.php路徑的方法,十分的簡單實用,思路也很巧妙,有需要的小伙伴可以參考下2016-05-05PHP中PDO連接數(shù)據(jù)庫中各種DNS設置方法小結(jié)
這篇文章主要介紹了PHP中PDO連接數(shù)據(jù)庫中各種DNS設置方法,結(jié)合實例形式總結(jié)分析了php常用的各種pdo連接數(shù)據(jù)庫技巧,需要的朋友可以參考下2016-05-05php在apache環(huán)境下實現(xiàn)gzip配置方法
這篇文章主要介紹了php在apache環(huán)境下實現(xiàn)gzip配置方法,較為詳細的分析了相關配置文件的修改技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04生成靜態(tài)頁面的php函數(shù),php愛好者站推薦
生成靜態(tài)頁面的php函數(shù),php愛好者站推薦...2007-03-03PHP var_dump遍歷對象屬性的函數(shù)與應用代碼
var_dump此函數(shù)顯示關于一個或多個表達式的結(jié)構(gòu)信息,包括表達式的類型與值。數(shù)組將遞歸展開值,通過縮進顯示其結(jié)構(gòu)。2010-06-06