Home > Article > Backend Development > Source code analysis of YII (2)
The previous article briefly analyzed the process of Yii, from creating an application to outputting the results on the screen. This time I will do a slightly more complicated one, focusing on the output. It is no longer a simple line of "hello world", but needs to be processed by the view layer.
Still in the demos directory, this time we choose hangman, a simple guessing game. The old rule is to start at the entrance.
index.php:
<?<span>php </span><span>//</span><span> change the following paths if necessary</span> <span>$yii</span>=<span>dirname</span>(<span>__FILE__</span>).'/../../framework/yii.php'<span>; </span><span>$config</span>=<span>dirname</span>(<span>__FILE__</span>).'/protected/config/main.php'<span>; </span><span>//</span><span> remove the following line when in production mode // defined('YII_DEBUG') or define('YII_DEBUG',true);</span> <span>require_once</span>(<span>$yii</span><span>); Yii</span>::createWebApplication(<span>$config</span>)->run();
Compared with the helloworld application, this time there is main.php. Open main and look at the source code:
<?<span>php </span><span>return</span> <span>array</span><span>( </span>'name'=>'Hangman Game', 'defaultController'=>'game', 'components'=><span>array</span><span>( </span>'urlManager'=><span>array</span><span>( </span>'urlFormat'=>'path', 'rules'=><span>array</span><span>( </span>'game/guess/<g:\w>'=>'game/guess',<span> )</span>,<span> )</span>,<span> )</span>,<span> );</span>
In our future actual projects, we will often use configuration files, so I think it is necessary to understand the yii configuration file-main.php
'name'=>'This usually defines the title of the website', which is the title displayed on the web page when we open index.php.
'defaultController'=>'This is the default controller', which is the controller used by the system when there is no controller specified behind our index.php. If we don't point it out here, the default is site
'components'=>'Here are the parameters of the component, configured with a multi-dimensional array. 'For specific parameters, please view the yii manual.
Yii::createWebApplication($config)->run(); We have analyzed it in detail last time, here is a simple walk through:
CWebApplication.php -> CApplication.php -> __construct($config) :
<span>$this</span>-><span>preinit(); </span><span>$this</span>-><span>initSystemHandlers(); </span><span>$this</span>-><span>registerCoreComponents(); </span><span>$this</span>->configure(<span>$config</span><span>); </span><span>$this</span>->attachBehaviors(<span>$this</span>-><span>behaviors); </span><span>$this</span>-><span>preloadComponents(); </span><span>$this</span>->init();
We didn’t have a configuration process last time, so $this->configure($config) did nothing, but this time there are configuration parameters, so let’s go in and see what operations yii has done:
CApplication itself does not implement the configure method, it is inherited from CModule.php:
<span>public</span> <span>function</span> configure(<span>$config</span><span>) { </span><span>if</span>(<span>is_array</span>(<span>$config</span><span>)) { </span><span>foreach</span>(<span>$config</span> <span>as</span> <span>$key</span>=><span>$value</span><span>) </span><span>$this</span>-><span>$key</span>=<span>$value</span><span>; } }</span>
The code is very simple, that is, the key of the configuration parameter is used as the attribute name of the class, and the value is used as the attribute value of the class. After completing this process, run the run method on CApplication.
<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>
As we said before, just focus on $this->processRequest(); here. The result of the operation is to execute $this->runController('');
<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>
Since the url is index.php, there are no parameters after it, so it uses the default controller, which is the game we set in main.php. So $controller is equal to controllers/gameController.php, through the last From source code analysis, we can know that when there is no init method in gameController.php, the default method defined in the parent class is used (actually an empty method),
$controller->run($actionID<span>); == gameController</span>->run(''); gameController上没有实现run方法,于是又是去父类中找run
It can be seen from class GameController extends CController that the parent class is CController and find the corresponding run method:
<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>
It has been analyzed before. If it is not specified, it will be the default parameter. Then $actionID at this time is empty, and actionID is the default action defined in gameController: public $defaultAction='play';
runActionWithFilters ---> runAction --> $action->runWithParams<br>这里的$action 需要从CAction -> CInlineAction中去找<br>
<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>
After going through so many processes, it is similar to the process of hello world. According to the last analysis, we can know that it is implemented here
$controller->$methodName<span>(); 也就是GameController->actionPlay()<br>到此,我们本节的重点才真正开始:<br></span>
<span>public</span> <span>function</span><span> actionPlay() { </span><span>static</span> <span>$levels</span>=<span>array</span><span>( </span>'10'=>'Easy game; you are allowed 10 misses.', '5'=>'Medium game; you are allowed 5 misses.', '3'=>'Hard game; you are allowed 3 misses.',<span> ); </span><span>//</span><span> if a difficulty level is correctly chosen</span> <span>if</span>(<span>isset</span>(<span>$_POST</span>['level']) && <span>isset</span>(<span>$levels</span>[<span>$_POST</span>['level'<span>]])) { </span><span>$this</span>->word=<span>$this</span>-><span>generateWord(); </span><span>$this</span>->guessWord=<span>str_repeat</span>('_',<span>strlen</span>(<span>$this</span>-><span>word)); </span><span>$this</span>->level=<span>$_POST</span>['level'<span>]; </span><span>$this</span>->misses=0<span>; </span><span>$this</span>->setPageState('guessed',<span>null</span><span>); </span><span>//</span><span> show the guess page</span> <span>$this</span>->render('guess'<span>); } </span><span>else</span><span> { </span><span>$params</span>=<span>array</span><span>( </span>'levels'=><span>$levels</span>, <span>//</span><span> if this is a POST request, it means the level is not chosen</span> 'error'=>Yii::app()->request->isPostRequest,<span> ); </span><span>//</span><span> show the difficulty level page</span> <span>$this</span>->render('play',<span>$params</span><span>); } }</span>
<span>显然走的是else的逻辑,重点请看</span> $this->render('play',$params); 这个render方法这么面熟,很多框架中都有类似的方法,比如discuz,smarty,CI 等等. 纵观yii框架,rnder 在它整个MVC模式中,是V得以实现的重要骨干。所以有必要把它翻个底朝天。<br>在CController.php中有这个方法:
<span>public</span> <span>function</span> render(<span>$view</span>,<span>$data</span>=<span>null</span>,<span>$return</span>=<span>false</span><span>) { </span><span>if</span>(<span>$this</span>->beforeRender(<span>$view</span><span>)) { </span><span>$output</span>=<span>$this</span>->renderPartial(<span>$view</span>,<span>$data</span>,<span>true</span><span>); </span><span>if</span>((<span>$layoutFile</span>=<span>$this</span>->getLayoutFile(<span>$this</span>->layout))!==<span>false</span><span>) </span><span>$output</span>=<span>$this</span>->renderFile(<span>$layoutFile</span>,<span>array</span>('content'=><span>$output</span>),<span>true</span><span>); </span><span>$this</span>->afterRender(<span>$view</span>,<span>$output</span><span>); </span><span>$output</span>=<span>$this</span>->processOutput(<span>$output</span><span>); </span><span>if</span>(<span>$return</span><span>) </span><span>return</span> <span>$output</span><span>; </span><span>else</span> <span>echo</span> <span>$output</span><span>; } }</span>
When we echo $output=$this->renderPartial($view,$data,true);, we find that $output at this time has already obtained our final result. Its corresponding file is views/game/play.php
This is what we finally see on index.php. Since this rendering is relatively simple, the program goes through fewer processes, but as you can see from the source code, a lot of processing has been done, such as themes and so on. This time we will analyze it first. Good night!
<br>
The above introduces the source code analysis of YII (2), including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.