Yii2 Source Learning Notes (20), Yii2 Source Learning notes
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 */7 8 namespaceYii\Base;9 Ten Use Yii; One Use 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 */ + Public Static$counter =0; - /** the * @var string The prefix to the automatically generated widget IDs. * * @see getId () auto-generated prefixes $ */Panax Notoginseng Public Static$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 */ + Public Static$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 */ - Public Staticfunction 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 Public Staticfunction End () the { the if(!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 the if(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 the return$widget; +}Else { - Throw NewInvalidcallexception ("expecting end () of". Get_class ($widget).", found". Get_called_class ()); the }Bayi}Else { the Throw NewInvalidcallexception ("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 Public StaticFunction 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 Parts101 102 returnOb_get_clean (). $ out;//returns the contents of the internal buffer, closing the buffer103 }104 the Private$_id;106 107 /**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 { the if($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 }122 123 /**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 the Private$_view;133 134 /**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 {144 if($ This->_view = = =NULL) {145$ This->_view = Yii:: $app->getview ();//call GetView () to get the View object instance if the View object is empty146 }147 148 return$ This-_view;149 } Max 151 /** 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 }168 169 /** 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 view192 return$ This->getview ()->render ($view, $params, $ This);193 }194 195 /**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 {204 return$ This->getview ()->renderfile ($file, $params, $ This);205 }206 207 /**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 directory216 returnDirName ($class->getfilename ()). Directory_separator.' views';217 }218}
http://www.bkjia.com/PHPjc/1137258.html www.bkjia.com true http://www.bkjia.com/PHPjc/1137258.html techarticle yii2 Source Learning Note (20), the Yii2 source Learning Note Widget class is the base class for all parts. yii2\base\widget.php 1? PHP 2/* * 3 * @link HTTP://WWW.YIIFRAMEWORK.COM/4 * @copyri ...