Yii\base\inlineaction
The process of tracking a command-line request
namespace Yii\base; UseYii;/** * Inlineaction represents a method that an action is defined as a controller. * The name of the Controller method is available via [[Actionmethod]] which * is set by the [[controller]] who creates this action. * * @author Qiang Xue <[email protected]> * @since 2.0*/classInlineactionextendsaction{/** * @var String The controller method that this-inline action is associated with*/ Public $actionMethod; /** * @param string $id The ID of this action * @param controller $controller The controller, the owns this ACTI On * @param string $actionMethod The controller method, the this-inline action is associated with * @param array $ Config Name-value pairs that'll be used to initialize the object properties*/ Public function__construct ($id,$controller,$actionMethod,$config= []) { //An instance of the Id,controller that passed in the action, the name of the action method, and the configuration of the action $this->actionmethod =$actionMethod; Parent:: __construct ($id,$controller,$config); } /** * This action is run with the specified parameters. * This method is primarily called by the Controller. * @param array $params action Parameters * @return Mixed the result of the action*/ Public functionRunwithparams ($params) { //gets the parameter of the action, which is obtained by merging the parameters of the entry and the default parameters $args=$this->controller->bindactionparams ($this,$params); Yii:: Trace (' Running action: '.Get_class($this->controller). ‘::‘ .$this->actionmethod. ‘()‘,__method__); if(Yii::$app->requestedparams = = =NULL) { //record the parameters of the requestYII::$app->requestedparams =$args; } //This action method that executes the controller return Call_user_func_array([$this->controller,$this->actionmethod],$args); }}
Controller call
Public function__construct ($id,$module,$config= []) {+//The ID of the incoming controller and an instance of the module in which it resides $this->id =$id; $this->module =$module; Parent:: __construct ($config); @@ -117,6 +118,7 @@ Public functionactions ()*/ Public functionRunaction ($id,$params= []) {+//create an instance of an action $action=$this->createaction ($id); if($action===NULL) { Throw NewInvalidrouteexception (' Unable to resolve the request: '.$this->getuniqueid (). ‘/‘ .$id); @@ -125,6 +127,7 @@ Public functionRunaction ($id,$params=[]) Yii:: Trace ("Route to Run:".)$action->getuniqueid (),__method__); if(Yii::$app->requestedaction = = =NULL) {+//record the current action as RequestedactionYII::$app->requestedaction =$action; } @@ -135,24 +138,31 @@ Public functionRunaction ($id,$params= []) $runAction=true; //Call beforeaction on modules+//beforeaction of module execution from the outside to the inside layer foreach($this->getmodules () as $module) { if($module->beforeaction ($action)) {+//The successful module is put into the $modules and the order is reversed Array_unshift($modules,$module); } Else {+//execution fails, mark it, and jump out of the loop $runAction=false; Break; } } $result=NULL; +//Modules's beforeaction executed successfully and beforeaction of the controller itself was successful. if($runAction&&$this->beforeaction ($action)) { //Run the Action+//Execute Action $result=$action->runwithparams ($params); +//execute the Afteraction of the controller itself $result=$this->afteraction ($action,$result); //Call afteraction on modules+//perform all modules beforeaction from the inside to the outer layers. foreach($modules as $module) { /*@var $module Module*/ $result=$module->afteraction ($action,$result); @@ -211,17 +221,25 @@ Public functionBindactionparams ($action,$params) Public functionCreateAction ($id) { if($id= = = "") {+//If the ID of the action is empty, the default action is used $id=$this-DefaultAction; } +//gets the actionmap of the definition in the Actions method $actionMap=$this-actions (); if(isset($actionMap[$id])) {+//If the ID of the action is in ActionMap, create the action returnYii::createobject ($actionMap[$id], [$id,$this]); } ElseIf(Preg_match('/^[a-z0-9\\-_]+$/',$id) &&Strpos($id, '--') = = =false&&Trim($id, '-') = = =$id) {+//if the ID conforms to the naming convention and does not exist--and neither side exists- -+//must be a number/lowercase letter/Right slash/middle dash/ underline+//stitching the name of the action method with a method similar to splicing the Controller class name $methodName= ' action '.Str_replace(‘ ‘, ‘‘,Ucwords(implode(‘ ‘,Explode(‘-‘,$id)))); if(method_exists($this,$methodName)) {+//If the method exists, a new Reflectionmethod instance $method=New\reflectionmethod ($this,$methodName); if($method->ispublic () &&$method->getname () = = =$methodName) {+//If the method is public, a new inlineaction returns return NewInlineaction ($id,$this,$methodName); } } @@ -297,9 +315,13 @@ Public functionAfteraction ($action,$result) */ Public functionGetModules () {+//an array of modules consisting of the current controller's module $modules= [$this-Module]; $module=$this-module;+//traverse the module's module until it is empty while($module->module!==NULL) {+//array_unshift-inserting one or more cells at the beginning of an array+//Insert the outside module at the beginning of the modules array Array_unshift($modules,$module-module); $module=$module-module; }
Learning yii2.0 Framework Read code (16)