Yii Source Analysis (II.), Yii source analysis
The previous article briefly analyzed the yii process, from creating an application to outputting the results on the screen. This time I come to a slightly more complicated, focused on the output, is no longer a simple line of "Hello World", but to go through the view layer of processing.
Still is the Demos directory, this time we choose Hangman, a simple guessing word game. Old rules, or starting at the entrance.
index.php:
PHP// Change the following paths if necessary$yii=dirname(__file__). ' /.. /.. /framework/yii.php '; $config=dirname(__file__). ' /protected/config/main.php '; // remove the following line while in production mode//defined (' Yii_debug ') or define (' Yii_debug ', true); require_once ($yii); Yii:: Createwebapplication ($config)->run ();
Compared with HelloWorld application, this time more main.php, open main to see the source:
PHPreturnarray( ' name ' = = ' Hangman game ', ' defaultcontroller ' = ' game ', ' components ' =array( ' urlmanager ' = =array( ' Urlformat ' + ' path ', ' rules ' = =array( ' game/guess/
' = ' Game/guess ',
),
),)
;
In the future of our actual project, but also often to use the configuration file, so I think it is necessary to understand the Yii configuration file--main.php
The ' name ' and ' = ' is usually defined as the title of the website, which is the title displayed on the page when we open index.php.
' Defaultcontroller ' = ' Here is the default controller ', which is the controller that the system uses when the controller is not specified behind our index.php, if we do not point out here, the default is the site
' Components ' = ' Here is the parameter of the component, which is configured with a multidimensional array. ' Specific parameters can be viewed in the Yii manual.
Yii::createwebapplication ($config)->run (); The last time we had a detailed analysis of it, here is a simple walk again:
cwebapplication.php, capplication.php, __construct ($config):
$this-PreInit (); $this-initsystemhandlers (); $this-registercorecomponents (); $this->configure ($config); $this->attachbehaviors ($this-behaviors); $this-preloadcomponents (); $this->init ();
The last time we had no configuration process, so $this->configure ($config) did nothing, but this time there are configuration parameters, so let's go inside to see what Yii did:
CApplication himself does not implement the Configure method, which is inherited from cmodule.php:
Public function Configure ($config) { if(is_array($config ) { foreach($configas$key= +$value ) $this$key=$value; } }
The code is very simple, that is, the key of the configuration parameter as the property name of the class, value is extended as the property value of the class. The Run method on the CApplication is running on the completion of this process.
Public function Run () { if($this->haseventhandler (' OnBeginRequest ')) $this- >onbeginrequest (new CEvent ($this)); register_shutdown_function (array($this, ' End '), 0,false); $this-ProcessRequest (); if ($this->haseventhandler (' onendrequest ')) $this->onendrequest (new CEvent ($this)); }
As we said before, just focus on $this->processrequest (); You can do it. The result of the operation is the execution of $this->runcontroller (");
Public functionRuncontroller ($route) { 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 NewChttpexception (404,yii::t (' Yii ', ' unable to resolve the request ' {route} ',Array(' {route} ' = =$route===''?$this->defaultcontroller:$route))); }
Since the URL is index.php and there are no parameters behind it, it is the default controller to go, that is, the game we set in main.php. So $controller equals controllers/gamecontroller.php, Through the last source code analysis, we can know that there is no Init method in gamecontroller.php, is the default method defined in the parent class to walk (actually an empty method),
$controller->run ($actionID); = = Gamecontroller->run ("); Gamecontroller did not implement the Run method, and then go to the parent class to find run
From class Gamecontroller extends Ccontroller you can see that the parent class is ccontroller and find the appropriate Run method:
public function Run ( $actionID ) { if< /span> (( $action = $this ->createaction ( $actionID )!== null ) { if ( $parent = $this ->getmodule ()) = = = = null< /span> ) $parent =yii:: app (); if ( $parent ->beforecontrolleraction ( $this , $action ) { $this ->runactionwithfilters ( $action , $this
-> filters ()); $parent ->aftercontrolleraction ( $this , $action ); }} else $this ->missingaction ( $actionID ); }
It has been parsed before and is the default parameter when not specified. Then the $actionid is empty, ActionId is the default action defined in Gamecontroller: public $defaultAction = ' play ';
Runactionwithfilters---> runaction-$action->runwithparams
The $action here need to be found from CAction-a cinlineaction.
Public function runwithparams ($params) { $methodName= ' action '. $this-getId (); $controller=$this-Getcontroller (); $method=new reflectionmethod ($controller$methodName); if ($method->getnumberofparameters () >0) return $this->runwithparamsinternal ($controller$method$params); Else return $controller$methodName(); }
The process of taking so many steps is similar to that of Hello World. According to the last analysis, it's done.
$controller-$methodName(); Gamecontroller->actionplay ()
Here, our focus in this section really begins:
Public functionActionplay () {Static $levels=Array( ' ' + ' easy game; You are allowed misses ', ' 5 ' = ' Medium ' game; You are allowed 5 misses ', ' 3 ' + ' hard game; You are allowed 3 misses. ', ); //If a difficulty level is correctly chosen if(isset($_post[' Level ']) &&isset($levels[$_post[' Level ']])) { $this->word=$this-Generateword (); $this->guessword=str_repeat('_',strlen($this-word)); $this->level=$_post[' Level ']; $this->misses=0; $this->setpagestate (' guessed ',NULL); //show The Guess page $this->render (' guess ')); } Else { $params=Array( ' Levels ' =$levels,//If this was a POST request, it means the level was not chosen' Error ' =>yii::app ()->request->ispostrequest, ); //show the difficulty level page $this->render (' Play ',$params); } }
obviously walking is the other's logic, focus on see $this->render (' play ', $params); This render method is so familiar that many frameworks have similar methods, such as Discuz,smarty,ci and so on. Throughout the YII framework, Rnder is an important backbone for V to achieve in its entire MVC model. So it's necessary to turn it upside down.
This method is available in ccontroller.php:
Public functionRender$view,$data=NULL,$return=false) { if($this->beforerender ($view)) { $output=$this->renderpartial ($view,$data,true); if(($layoutFile=$this->getlayoutfile ($this->layout))!==false) $output=$this->renderfile ($layoutFile,Array(' content ' =$output),true); $this->afterrender ($view,$output); $output=$this->processoutput ($output); if($return) return $output; Else Echo $output; } }
When we echo $output = $this->renderpartial ($view, $data, True), we find that the $output of this time has already obtained our final result. The file it corresponds to is views/game/play.php
That's what we finally saw on the index.php. Because this rendering is relatively simple, so the procedure is also less process, but from the source can be seen, inside a lot of processing, such as the theme or something. This is the first analysis of this. Good night!
http://www.bkjia.com/PHPjc/925548.html www.bkjia.com true http://www.bkjia.com/PHPjc/925548.html techarticle Yii Source Analysis (ii), Yii source analysis of a simple analysis of the yii process, from the creation of an application, to the screen output results. This time I come to a slightly more complicated, heavy ...