$ this- & gt; preinit () // 당분간 사용하지 않으며 추후 확장에 맡길 것으로 추정됩니다 $this->initSystemHandlers();//오류 처리 설정 $this->registerCoreComponents();//핵심 컴포넌트 등록 $this->configure($config); //구성 파일을 통해 클래스의 속성을 확장하고 비어 있으면 아무것도 하지 않습니다 $this->attachBehaviors($this->behaviors); $this->preloadComponents(); $this->init(); } $this 아래의 일부 메소드는 상위 클래스에서 올 수 있으므로 현재 클래스에서 찾을 수 없습니다. 그렇지 않은 경우 ctrl+f를 사용하여 검색하는 것입니다. 확인해보세요 : 추상 클래스 CApplication은 CModule을 확장합니다. 분명히 CModule에 들어가야 합니다. 여전히 원하는 방법을 찾을 수 없으면 이전 프로세스를 계속하세요. 추상 클래스 CModule은 CComponent를 확장합니다 CComponent 클래스까지 이곳이 현재 이들의 본거지라는 뜻이다. 코드의 주석에서도 확인할 수 있습니다. CComponent는 모든 구성요소의 기본 클래스입니다 제 생각이 맞다는 뜻입니다. 네, 기본 클래스입니다. 코드를 통해 현재 애플리케이션에서 많은 매개변수가 비어 있기 때문에 많은 로직을 직접 건너뛰었음을 알 수 있습니다. 이것을 보면 $this의 내용은 대략적으로 알 수 있습니다. 다시 돌아보자 새 CWebApplication($config)->run() 반환; 이전의 레이어별 분석을 통해 이때의 실행 방식도 쉽게 찾아볼 수 있다. CApplication 바로 내부:
以上就介绍了YII 的源码分析(-),包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。
<spanmicrosoft yahei font-size:><span>public</span> <span>function</span> __construct(<span>$config</span>=<span>null</span><span>)
{
Yii</span>::setApplication(<span>$this</span><span>);
</span><span>//</span><span> set basePath at early as possible to avoid trouble</span>
<span>if</span>(<span>is_string</span>(<span>$config</span><span>))
</span><span>$config</span>=<span>require</span>(<span>$config</span><span>);
</span><span>if</span>(<span>isset</span>(<span>$config</span>['basePath'<span>]))
{
</span><span>$this</span>->setBasePath(<span>$config</span>['basePath'<span>]);
</span><span>unset</span>(<span>$config</span>['basePath'<span>]);
}
</span><span>else</span>
<span>$this</span>->setBasePath('protected'<span>);
Yii</span>::setPathOfAlias('application',<span>$this</span>-><span>getBasePath());
Yii</span>::setPathOfAlias('webroot',<span>dirname</span>(<span>$_SERVER</span>['SCRIPT_FILENAME'<span>]));
</span><span>if</span>(<span>isset</span>(<span>$config</span>['extensionPath'<span>]))
{
</span><span>$this</span>->setExtensionPath(<span>$config</span>['extensionPath'<span>]);
</span><span>unset</span>(<span>$config</span>['extensionPath'<span>]);
}
</span><span>else</span><span>
Yii</span>::setPathOfAlias('ext',<span>$this</span>->getBasePath().DIRECTORY_SEPARATOR.'extensions'<span>);
</span><span>if</span>(<span>isset</span>(<span>$config</span>['aliases'<span>]))
{
</span><span>$this</span>->setAliases(<span>$config</span>['aliases'<span>]);
</span><span>unset</span>(<span>$config</span>['aliases'<span>]);
}</span></spanmicrosoft>
<spanmicrosoft yahei font-size:><span>public</span> <span>function</span><span> run()
{
</span><span>if</span>(<span>$this</span>->hasEventHandler('onBeginRequest'<span>)){
</span><span>$this</span>->onBeginRequest(<span>new</span> CEvent(<span>$this</span><span>));
}
</span><span>register_shutdown_function</span>(<span>array</span>(<span>$this</span>,'end'),0,<span>false</span><span>);
</span><span>$this</span>-><span>processRequest();
</span><span>if</span>(<span>$this</span>->hasEventHandler('onEndRequest'<span>)){
</span><span>$this</span>->onEndRequest(<span>new</span> CEvent(<span>$this</span><span>));
}
}</span></spanmicrosoft>
<spanmicrosoft yahei font-size:><span>abstract</span> <span>public</span> <span>function</span> processRequest(); 这个方法在当前类中是抽象的,所以肯定在它的子类中实现了。回去找CWebApplication:
<span>public</span> <span>function</span><span> processRequest()
{
</span><span>if</span>(<span>is_array</span>(<span>$this</span>->catchAllRequest) && <span>isset</span>(<span>$this</span>->catchAllRequest[0<span>]))
{
</span><span>$route</span>=<span>$this</span>->catchAllRequest[0<span>];
</span><span>foreach</span>(<span>array_splice</span>(<span>$this</span>->catchAllRequest,1) <span>as</span> <span>$name</span>=><span>$value</span><span>)
</span><span>$_GET</span>[<span>$name</span>]=<span>$value</span><span>;
}
</span><span>else</span>
<span>$route</span>=<span>$this</span>->getUrlManager()->parseUrl(<span>$this</span>-><span>getRequest());
</span><span>$this</span>->runController(<span>$route</span><span>);
}</span></spanmicrosoft>
<spanmicrosoft yahei font-size:><span>public</span> <span>function</span> runController(<span>$route</span><span>)
{
</span><span>if</span>((<span>$ca</span>=<span>$this</span>->createController(<span>$route</span>))!==<span>null</span><span>)
{
</span><span>list</span>(<span>$controller</span>,<span>$actionID</span>)=<span>$ca</span><span>;
</span><span>$oldController</span>=<span>$this</span>-><span>_controller;
</span><span>$this</span>->_controller=<span>$controller</span><span>;
</span><span>$controller</span>-><span>init();
</span><span>$controller</span>->run(<span>$actionID</span><span>);
</span><span>$this</span>->_controller=<span>$oldController</span><span>;
}
</span><span>else</span>
<span>throw</span> <span>new</span> CHttpException(404,Yii::t('yii','Unable to resolve the request "{route}".',
<span>array</span>('{route}'=><span>$route</span>===''?<span>$this</span>->defaultController:<span>$route</span><span>)));
}</span></spanmicrosoft>
<spanmicrosoft yahei font-size:><span>public</span> <span>function</span> run(<span>$actionID</span><span>)
{
</span><span>if</span>((<span>$action</span>=<span>$this</span>->createAction(<span>$actionID</span>))!==<span>null</span><span>)
{
</span><span>if</span>((<span>$parent</span>=<span>$this</span>->getModule())===<span>null</span><span>){
</span><span>$parent</span>=Yii::<span>app();
}
</span><span>if</span>(<span>$parent</span>->beforeControllerAction(<span>$this</span>,<span>$action</span><span>))
{
</span><span>$this</span>->runActionWithFilters(<span>$action</span>,<span>$this</span>-><span>filters());
</span><span>$parent</span>->afterControllerAction(<span>$this</span>,<span>$action</span><span>);
}
}
</span><span>else</span>
<span>$this</span>->missingAction(<span>$actionID</span><span>);
}
</span></spanmicrosoft>
<spanmicrosoft yahei font-size:><span>public</span> <span>function</span> runActionWithFilters(<span>$action</span>,<span>$filters</span><span>)
{
</span><span>if</span>(<span>empty</span>(<span>$filters</span><span>)){
</span><span>$this</span>->runAction(<span>$action</span><span>);
}
</span><span>else</span><span>
{
</span><span>$priorAction</span>=<span>$this</span>-><span>_action;
</span><span>$this</span>->_action=<span>$action</span><span>;
CFilterChain</span>::create(<span>$this</span>,<span>$action</span>,<span>$filters</span>)-><span>run();
</span><span>$this</span>->_action=<span>$priorAction</span><span>;
}
}</span></spanmicrosoft>
<spanmicrosoft yahei font-size:><span>public</span> <span>function</span> runAction(<span>$action</span><span>)
{
</span><span>$priorAction</span>=<span>$this</span>-><span>_action;
</span><span>$this</span>->_action=<span>$action</span><span>;
</span><span>if</span>(<span>$this</span>->beforeAction(<span>$action</span><span>))
{
</span><span>if</span>(<span>$action</span>->runWithParams(<span>$this</span>->getActionParams())===<span>false</span><span>){
</span><span>$this</span>->invalidActionParams(<span>$action</span><span>);
}
</span><span>else</span><span>{
</span><span>$this</span>->afterAction(<span>$action</span><span>);
}
}
</span><span>$this</span>->_action=<span>$priorAction</span><span>;
}</span></spanmicrosoft>
<spanmicrosoft yahei font-size:> <span>public</span> <span>function</span> runWithParams(<span>$params</span><span>)
{
</span><span>$methodName</span>='action'.<span>$this</span>-><span>getId();
</span><span>$controller</span>=<span>$this</span>-><span>getController();
</span><span>$method</span>=<span>new</span> ReflectionMethod(<span>$controller</span>, <span>$methodName</span><span>);
</span><span>if</span>(<span>$method</span>->getNumberOfParameters()>0<span>)
</span><span>return</span> <span>$this</span>->runWithParamsInternal(<span>$controller</span>, <span>$method</span>, <span>$params</span><span>);
</span><span>else</span>
<span>return</span> <span>$controller</span>-><span>$methodName</span><span>();
}</span></spanmicrosoft>
<spanmicrosoft yahei font-size:> <span>public</span> <span>function</span><span> actionIndex()
{
</span><span>echo</span> 'Hello World'<span>;
}</span></spanmicrosoft>