Learning yii2.0 Framework Read code (19)

Source: Internet
Author: User

Vendor/yiisoft/yii2/base/module. PHP (cont.)

    /** * Retrieves the specified sub-module ID.     * This method supports retrieving two sub-modules and submodules. * @param string $id module ID (case-sensitive).     To retrieve grand child modules, * The use ID of path relative to the this module (e.g. ' admin/content ').     * @param boolean $load whether to load the module if it's not yet loaded.     * @return Module|null The module instance, null if the module does not exist. * @see HasModule ()*/     Public functionGetModule ($id,$load=true)    {        if(($pos=Strpos($id, '/'))!==false) {            //string $id (case-sensitive). Retrieving sub-modules            $module=$this->getmodule (substr($id, 0,$pos)); //determine whether to load            return $module===NULL?NULL:$module->getmodule (substr($id,$pos+ 1),$load); }        if(isset($this->_modules[$id])) {            //determine if $id exists            if($this->_modules[$id] instanceof Module) {return $this->_modules[$id]; } ElseIf($load) {Yii:: Trace ("Loading module:$id",__method__); //to view $id by returning a class                $module= Yii::createobject ($this->_modules[$id], [$id,$this]); $module->setinstance ($module); return $this->_modules[$id] =$module; }        }        //does not exist return null        return NULL; }    /** * Adds a sub-module to this module. * @param string $id module ID * @param module|array|null $module The sub-module to is added to this module. This can * is one of the followings: * *-A [[Module]] Object *-a configuration array:when [[GetModule ()]] is called initially, the array * would be used to instantiate the Sub-module *-Null:the named Sub-module would be removed from the This module*/     Public functionSetmodule ($id,$module)    {   //Define a sub-module        if($module===NULL) {            //if it is empty, remove from this sub-module            unset($this->_modules[$id]); } Else {            //otherwise assign to it            $this->_modules[$id] =$module; }    }    /** * Register sub-module in current module. * * Each submodule should be specified as a name-value pair * Name refers to the module and value module ID or configuration * array that can is used to create the module.     In the latter case, [[Yii::createobject ()]] * would be used to create the module.     * * If A new sub-module have the same ID as an existing one, the existing one would be overwritten silently.     * * The following is a example for registering and the sub-modules: * * ~ ~ ~ ~ [* ' Comment ' = [ * ' class ' = ' app\modules\comment\commentmodule ', * ' db ' = ' db ', *], * ' Boo King ' = [' class ' = ' App\modules\booking\bookingmodule '], * * ~ ~ ~ * @param array $modules modul ES (id = module configuration or instances)*/     Public functionSetmodules ($modules)    {        foreach($modules  as $id=$module) {            $this->_modules[$id] =$module; }    }    /** * This method parses the specified route and creates the corresponding submodule (s), Controller and action * This method parses the specified route and creates the corresponding child Module (s), Controller and action * instances.     It then calls [[Controller::runaction ()]] to run the action with the given parameters.     * If the path is empty, the method uses [[Defaultroute]] * @param string $route The route that specifies the action.     * @param array $params The parameters to being passed to the action * @return mixed the result of the action. * @throws Invalidrouteexception If the requested route cannot be resolved to an action successfully*/     Public functionRunaction ($route,$params= [])    {        //Create the controller, get the instance of the controller and the ID of the action        $parts=$this->createcontroller ($route); if(Is_array($parts)) {            /*@var $controller Controller*/            List($controller,$actionID) =$parts; //keep the controller that is bound in the app            $oldController= Yii::$app-Controller; //bind the current controller to the appYII::$app->controller =$controller; $result=$controller->runaction ($actionID,$params); //Restore the controller that was previously bound to the appYII::$app->controller =$oldController; return $result; } Else {            $id=$this-Getuniqueid (); Throw NewInvalidrouteexception (' Unable to resolve the request '. ($id= = = '?$route:$id. ‘/‘ .$route) . ‘".‘); }    }

Learning yii2.0 Framework Read code (19)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.