This example describes the YII2 creation controller (Createcontroller) method. Share to everyone for your reference, as follows:
In Yii, the controller is created in the application request through Urlmanager parsing the way out by the information, and then by Yii\base\module in the
Public Function Runaction ($route, $params = [])
method to create the controller, and finally the controller performs the corresponding action.
The first thing to make clear is that therouting in Yii is divided into three scenarios:
The first is the module Id/controller id/action ID),
The second is a (sub dir)/controller id/action ID with a namespace (subdirectory)
The third is only controllers and actions (Controller id/action ID)
These three have precedence, so when the controller is created, it is also first to see if the module type of the route, if so, then get the module, and then the module to create the controller
Then you can determine if it is the second type with a namespace.
Public Function Createcontroller ($route) {//If the route is empty, use the default route if ($route = = = ") {$route = $this->defaultroute; }//double slashes or leading/ending slashes may cause substr problem//Remove the trailing backslash ("/") and return False if the route contains "//". $route = Trim ($route, '/'); if (Strpos ($route, '//')!== false) {return false; }/* * Route in three cases, * one is with module ID (module Id/controller id/action ID), * One is a namespace (sub dir)/controller id/action ID) * One is only controllers and actions (Controller id/action ID) * So here to separate into two parts according to the first "/", $id and $route information, */if (Strpos ($route, '/')!== false) {List ($id, $route) = explode ('/', $route, 2); } else {$id = $route; $route = "; }//module and controller map take precedence/* * To see if this ID is a module, and if it is a module, then use this module to create the controller. * Therefore, if a controller's name and module name are duplicated, the controller inside the module will be created first. * * If there is url:http://www.yii2.com/index.php?r=test/index * Originally intended to access the controller inside the application in the test controller, perform the index action. * * However if there is a module named Test, there is a indexcontroller * * According to the above will generate $id=test, $route =index * * Due to find in the following existThis module will execute the index controller under this test module, * without executing the index action of the test controller inside the application */$module = $this->getmodule ($id); if ($module!== null) {return $module->createcontroller ($route); }//If a controller mapping is specified in the CONTROLLERMAP array, the controller if (Isset ($this->controllermap[$id]) is created based on the mappings inside it {$controller = Yii::crea Teobject ($this->controllermap[$id], [$id, $this]); return [$controller, $route]; }/* If there is a "/" in $route at this time, that is, the original route is HOME/INDEX/AA * $id: Home (not module) * $route: INDEX/AA * Because through the above know that home is not a module, so this is the namespace (subdirectories), * * $id: Home/index namespace (subdirectories) home under the index controller * $route: AAA */if ($pos = Strrpos ($route, !== false) {$id. = '/'. substr ($route, 0, $pos); $route = substr ($route, $pos + 1); }/* * $id: Home/index * $route: AAA */$controller = $this->createcontrollerbyid ($id); if ($controller = = = null && $route!== ') {//If creation fails, plus route as ID is created again $controller = $this->createcontr Ollerbyid ($id. '/' . $route); $route = "; } return $controller = = = null? False: [$controller, $route];}
There are two cases of $id in this function, one of which is preceded by a namespace, and one is directly on the controller ID.
Public Function Createcontrollerbyid ($id) {if (!preg_match ('%^[a-z0-9\\-_/]+$% ', $id)) {return null; }/* * If there is "/" in the $id, the front is the directory, followed by the class */$pos = Strrpos ($id, '/'); if ($pos = = = False) {$prefix = '; $className = $id; } else {$prefix = substr ($id, 0, $pos + 1); $className = substr ($id, $pos + 1); }//Generate Controller class Indexcontroller $className = Str_replace (', ', ' Ucwords (Str_replace ('-', ' ', $className))). ' Controller '; If there is a prefix (that is, there is a directory, namespace), precede the class with a namespace $className = LTrim ($this->controllernamespace. '\\' . Str_replace ('/', ' \ \ ', $prefix). $className, ' \ \ '); If the class does not exist, or if the class name contains "-", then an error occurs, if (Strpos ($className, '-')!== false | |!class_exists ($className)) {return null; }//The following is the creation of the class if (Is_subclass_of ($className, ' Yii\base\controller ')) {return new $className ($id, $this); } elseif (Yii_debug) {throw new Invalidconfigexception ("Controller class must extend from \\yii\\base\\Controller.") ); } else {return null; }}
The process is over, and then the created controller executes the action inside it.
Public Function Runaction ($route, $params = []) { $parts = $this->createcontroller ($route); if (Is_array ($parts)) { /** @var Controller $controller */ list ($controller, $actionID) = $parts; $oldController = Yii:: $app->controller; Yii:: $app->controller = $controller; The controller performs the corresponding action $result = $controller->runaction ($actionID, $params); Yii:: $app->controller = $oldController; return $result; } else { $id = $this->getuniqueid (); throw new Invalidrouteexception (' Unable to resolve the request '. ($id = = = "? $route: $id. '/' . $route). '".'); }}
For more information on YII related content readers can view this site topic: "YII framework Introduction and common skills Summary", "PHP Excellent Development Framework Summary", "Smarty Template Primer Basic Tutorial", "PHP Object-oriented Programming tutorial", "PHP string (String) Usage Summary "," Introduction to Php+mysql Database Operation "and" PHP common database Operation Skills Summary "
It is hoped that this article is helpful to the PHP program design based on YII framework.