Android Source Learning yii2 Source Learning Note 20)

Source: Internet
Author: User
Tags yii
The Widget class is the base class for all parts. yii2\base\widget.php

1 
 Php2/**3* @linkhttp://www.yiiframework.com/4* @copyright Copyright (c) Yii software LLC5* @licensehttp://www.yiiframework.com/license/6*/78namespaceYii\Base;9TenUse Yii; OneUse reflectionclass; A -/** -* Widgets are the base class for widgets. the* Widget is the base class for all widgets -* @property string $id ID of the widget. Widget Identification -* @property \yii\web\view $view The "View object" can used to render views or View files. Note that the -* Type of this property differs in getter and setter. see [[GetView ()]] and [[Setview () ]] for details. +* The View object used to render the view or view file is different in getter and setter -* @property string $viewPath The directory containing the view files for this widget. This property is +* read-only. The view file directory that contains this control A * at* @author Qiang Xue 
     -* @since 2.0 -*/ -classWidget extends Component implements Viewcontextinterface -{ -/** in* @var integer a counter used to generate [[ID]] for widgets. -* @internal counters for generating widget IDs to*/ + PublicStatic$counter =0; -/** the* @var string The prefix to the automatically generated widget IDs. ** @see getId () auto-generated prefixes $*/Panax Notoginseng PublicStatic$autoIdPrefix ='W'; -/** the* @var widget[] The widgets that is currently being rendered (not ended). this property +* is maintained by [[begin ()]] and [[end ()]] methods. Widgets currently being rendered A* @internal the*/ + PublicStatic$stack = []; - $ $/** -* begins a widget. Start a part -* This method creates a instance of the calling class. It'll apply the configuration the* to the created instance. A matching [[end ()]] call should is called later. -* Create an instance of the calling class from the application configuration file, corresponding to the [end ()] Square methodWuyi* @param array $config name-value pairs that'll be used to initialize the object properties the* Parameters for initializing properties -* @return Static the newly created widget instance a newly created part instance Wu*/ - PublicStaticfunction begin ($config = []) About    { $$config ['class'] = Get_called_class ();//name of late static binding class -/*@var $widget Widgets*/ -$widget = Yii::createobject ($config);//instantiating the calling class through the class name and the incoming configuration -Static:: $stack [] = $widget;//Placing an object in the part stack that is being rendered A +return$widget; the    } - $/** the* Ends a widget. End Widget the* Note that the rendering result of the widget was directly echoed out. Rendering results are directly echoed the* @return Static The widget instance that is ended. Static end of the part instance.  the* @throws invalidcallexception if [[begin ()]] and [[End () ]] calls is not properly nested -*/ in PublicStaticfunction End () the    { theif(!empty (Static:: $stack)) {//invoking class instance exists in the rendered widget stack About$widget = Array_pop (Static:: $stack);//Remove the last instance from the stack theif(Get_class ($widget) = = =Get_called_class ()) { theecho $widget->run ();//If the deleted instance class name and the current calling class name are the same, the contents of the output widget thereturn$widget; +}Else { -ThrowNewInvalidcallexception ("expecting end () of". Get_class ($widget).", found". Get_called_class ()); the            }Bayi}Else { theThrowNewInvalidcallexception ("unexpected". Get_called_class ().':: End () call. A matching begin () is not found.'); the        } -    } - the/** the* Creates a widget instance and runs it. Create a part instance and run the* The widget rendering result is a returned by this method. Returns the result of the part rendering.  the* @param array $config name-value pairs that'll be used to initialize the object properties -* Parameters for initializing object Properties the* @return String The rendering result of the widget. Render results for the control.  the*/ the PublicStaticFunction Widget ($config = [])94    { theOb_start ();//Open Output Buffers theOb_implicit_flush (false);//Turn off absolute refresh the/*@var $widget Widgets*/98$config ['class'] = Get_called_class ();//gets the class name of the calling class About$widget = Yii::createobject ($config);//Instantiating Classes -$ out= $widget->run ();//Running Parts101102returnOb_get_clean (). $ out;//returns the contents of the internal buffer, closing the buffer103    }104 thePrivate$_id;106107/**108* Returns The ID of the widget. Returns the identity of the plug-in109* @param boolean $autoGenerate whether to generate an ID if it's not set previously the* Whether to generate a unique identity if not set111* @return string ID of the widget. Part Unique identification the*/113 Publicfunction GetId ($autoGenerate =true) the    { theif($autoGenerate && $ This->_id = = =NULL) { the//automatically generated if the identity is empty and is set to allow automatic generation of identities117$ This->_id =Static:: $autoIdPrefix.Static:: $counter + +;118        }119 -return$ This-_id;121    }122123/**124* Sets the ID of the widget. Setting the widget ID the* @param string $value ID of the widget. 126*/127 Publicfunction SetId ($value) -    {129$ This->_id =$value; the    }131 thePrivate$_view;133134/**135* Returns The View object that can is used to render views or view files. Return to view objects136* The [[Render ()]] and [[RenderFile () ]] methods'll use137* This view is object to implement the actual view rendering.138* [Render ()] and [RenderFile ()] methods implement the actual view display with the View object. 139* If not set, it would default to the "View" application component. $* @return \yii\web\view The View object, can be used to render views or View files.141*/142 Publicfunction GetView ()143    {144if($ This->_view = = =NULL) {145$ This->_view = Yii:: $app->getview ();//call GetView () to get the View object instance if the View object is empty146        }147148return$ This-_view;149    } Max151/** the* Sets the View object to being used by this widget. Set instance of view objects called by the current part153* @param view $view The View object, can be used to render views or View files.154*/155 Publicfunction Setview ($view)156    {157$ This->_view = $view;//the View object to use158    }159 the/**161* Executes the widget. Actuator Parts162* @return String The result of widget execution to be outputted.163* The result output of the control execution. 164*/165 Publicfunction Run ()166    {167    }168169/** the* Renders a view.171* The view to is rendered can specified in one of the following formats:172* Render a view actually calling a view in the view class rendered with the same name method can specify the path in the following ways173*-Path alias (e.g. "@app/views/site/index");174*-Absolute path within application (e.g. "//site/index"): The view name starts with double slashes.175* The actual view file is looked for under the [[Application::viewpath|view Path]] of the APPLICATION.176*-Absolute path within module (e.g. "/site/index"): The view name starts with a single slash.177* The actual view file is looked for under the [[Module::viewpath|view Path]] of the currently178* Active module.179*-relative path (e.g. "index"): The actual view file is looked for under [[ViewPath]]. the     *181* If The view name does not contain a file extension, it'll use the default one '. php '.182     *183* @param string $view the view name. View name184* @param array $params The parameters (Name-value pairs) that should is made available in the view.185* Parameters available in the View186* @return string The rendering result. Render Results187* @throws invalidparamexception If the view file does not exist.188*/189 Publicfunction Render ($view, $params= []) the    {191//call render render in the view class for the specified view192return$ This->getview ()->render ($view, $params, $ This);193    }194195/**196* Renders a view file.197* @param string $file The view file to be rendered. This can is either a file path or a path alias.198* @param array $params The parameters (Name-value pairs) that should is made available in the view.199* @return string The rendering result. $* @throws invalidparamexception If the view file does not exist.201*/202 Publicfunction RenderFile ($file, $params= [])203    {204return$ This->getview ()->renderfile ($file, $params, $ This);205    }206207/**208* Returns The directory containing the view files for this widget. Returns the view file path209* The default implementation returns the ' views ' subdirectory under the directory containing the Widget class Fil E. About* @return String The directory containing the view files for this widget.211*/212 Publicfunction Getviewpath ()213    {214$class=NewReflectionclass ($ This);215//get the directory of the part class file, stitching it into the view directory216returnDirName ($class->getfilename ()). Directory_separator.' views';217    }218}

The above describes the Android source learning yii2 Source Learning Note 20, including the Android source learning content, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    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.