yii框架学习笔记

简介:这是yii框架学习笔记的详细页面,介绍了和php,有关的知识、技巧、经验,和一些php源码等。

class='pingjiaF' frameborder='0' src='http://biancheng.dnbcw.info/pingjia.php?id=338751' scrolling='no'>
程序执行流程跟踪:

1 index.php

Yii::createWebApplication($config)->run(); //step 1 tracestep

2 yii/framework/yiibase.php

public static function createApplication($class,$config=null)
{
return new $class($config);//$class="CWebApplication";
}

3 yii/framework/web/CWebApplication.php

class CWebApplication extends CApplication

真正执行的是类CApplication的构造函数

yii/framework/base/CApplication.php

public function __construct($config=null)
{
//phptest($config);

Yii::setApplication($this); //设置当前程序实例给Yii类,方便通过Yii::app()应用

// set basePath at early as possible to avoid trouble
if(is_string($config))
$config=require($config);//得到配置参数,以数组形式返回给$config;
if(isset($config['basePath']))
{
//var_dump(realpath($config['basePath']));
$this->setBasePath($config['basePath']); 设置路径
unset($config['basePath']); //unset $config中的basePath;
}
else
$this->setBasePath('protected');


Yii::setPathOfAlias('application',$this->getBasePath());
Yii::setPathOfAlias('webroot',dirname($_SERVER['SCRIPT_FILENAME'])); 当前运行的程序的路径
Yii::setPathOfAlias('ext',$this->getBasePath().DIRECTORY_SEPARATOR.'extensions');//设置程序拓展路径

//var_dump(dirname($_SERVER['SCRIPT_FILENAME']));
$this->preinit();

$this->initSystemHandlers();
$this->registerCoreComponents();

$this->configure($config);将当前配置参数执行成类得属性
$this->attachBehaviors($this->behaviors);
$this->preloadComponents();

$this->init();
}

4 执行run()函数

public function run()
{
//debug_print_backtrace();//step 2 tracestep
//echo "zj";//
if($this->hasEventHandler('onBeginRequest'))
$this->onBeginRequest(new CEvent($this));
$this->processRequest(); //到CWebApplication.php 111 处理程序请求
if($this->hasEventHandler('onEndRequest'))
$this->onEndRequest(new CEvent($this));
}

5 yii/framework/web/CWebApplication.php

public function processRequest()
{
if(is_array($this->catchAllRequest) && isset($this->catchAllRequest[0]))
{
$route=$this->catchAllRequest[0];
foreach(array_splice($this->catchAllRequest,1) as $name=>$value)
$_GET[$name]=$value;
}
else
$route=$this->getUrlManager()->parseUrl($this->getRequest());


//debug_print_backtrace();
$this->runController($route); //step 3 tracestep
}

6 yii/framework/web/CWebApplication.php

public function runController($route)
{
if(($ca=$this->createController($route))!==null)
{
//debug_print_backtrace();
list($controller,$actionID)=$ca;
$oldController=$this->_controller;
$this->_controller=$controller;
$controller->init();
$controller->run($actionID); //step 4 tracestep 到framework/web/filters/cfilterchain.php 124
$this->_controller=$oldController;
}
else
throw new CHttpException(404,Yii::t('yii','Unable to resolve the request "{route}".',
array('{route}'=>$route===''?$this->defaultController:$route)));
}

7 上面的$controller->run($actionID)

真正执行的是yii/framework/web/CController.php的run

public function run($actionID)
{
//echo "zhoujian2";
if(($action=$this->createAction($actionID))!==null)
{
if(($parent=$this->getModule())===null)
$parent=Yii::app();
if($parent->beforeControllerAction($this,$action))
{
//debug_print_backtrace();
$this->runActionWithFilters($action,$this->filters());
$parent->afterControllerAction($this,$action);
}
}
else
$this->missingAction($actionID);
}

8 然后上面中的$this->runActionWithFilters($action,$this->filters());

在yii/framework/web/CController.php

public function runActionWithFilters($action,$filters)
{
if(empty($filters))
$this->runAction($action);//为空的话直接执行action
else
{
//debug_print_backtrace();
$priorAction=$this->_action;
$this->_action=$action;
CFilterChain::create($this,$action,$filters)->run();
$this->_action=$priorAction;
}
}

9 上面的CFilterChain::create($this,$action,$filters)->run();

在yii/framework/web/filters/CFilterChain.php

create函数里面首先穿件一个CFilterChain类得实例,然后处理并且,返回这个实例对象

然后调用刚才得到的CFilterChain对象的run方法

public function run()
{
if($this->offsetExists($this->filterIndex))
{
//debug_print_backtrace();
$filter=$this->itemAt($this->filterIndex++);
Yii::trace('Running filter '.($filter instanceof CInlineFilter ?
get_class($this->controller).'.filter'.$filter->name.'()':get_class($filter).'.filter()'),'system.web.filters.CFilterChain');
$filter->filter($this);//step 5 tracestep 到framework/web/filters/cinlinefilter.php 56
}
else
{
//debug_print_backtrace();
$this->controller->runAction($this->action); //step 9 tracestep 到framework/web/ccontroller.php 296
}

}

上面的大概意思如果有过滤就先过滤再执行controller->runAction();否则直接执行$this->runAction($action);//为空的话直接执行action


10

在yii/framework/web/CController.php

public function runAction($action)
{
//debug_print_backtrace();
$priorAction=$this->_action;
$this->_action=$action;
if($this->beforeAction($action))
{
if($action->runWithParams($this->getActionParams())===false)
//step 10 tracestep 到framework/web/actions/cinlineaction.php 42
$this->invalidActionParams($action);
else
$this->afterAction($action);
}
$this->_action=$priorAction;
}

11 上面的$action->runWithParams()

class CInlineAction extends CAction

yii/framework/web/actions/CInlineAction.php

public function runWithParams($params)
{
//debug_print_backtrace();
$methodName='action'.$this->getId();
$controller=$this->getController();
$method=new ReflectionMethod($controller, $methodName);
if($method->getNumberOfParameters()>0)
return $this->runWithParamsInternal($controller, $method, $params);
else
{
// phptest($methodName);
//step 11 tracestep 到framework/web/actions/cinlineaction.php 42
return $controller->$methodName();
//最终执行control中的方法(对应页面程序)/protect/controllers/SiteController.php的控制器和动作

}
}

爱J2EE关注Java迈克尔杰克逊视频站JSON在线工具

http://biancheng.dnbcw.info/php/338751.html pageNo:8

posted on 2011-11-12 09:20  圣者  阅读(212)  评论(0编辑  收藏  举报

导航