Class yii\base\Widget

继承yii\base\Widget » yii\base\Component » yii\base\BaseObject
实现yii\base\Configurable, yii\base\ViewContextInterface
子类yii\captcha\Captcha, yii\console\widgets\Table, yii\grid\GridView, yii\widgets\ActiveForm, yii\widgets\BaseListView, yii\widgets\Block, yii\widgets\Breadcrumbs, yii\widgets\ContentDecorator, yii\widgets\DetailView, yii\widgets\FragmentCache, yii\widgets\InputWidget, yii\widgets\LinkPager, yii\widgets\LinkSorter, yii\widgets\ListView, yii\widgets\MaskedInput, yii\widgets\Menu, yii\widgets\Pjax, yii\widgets\Spaceless
可用自版本2.0
源代码 https://github.com/yiisoft/yii2/blob/master/framework/base/Widget.php

Widget is the base class for widgets.

For more details and usage information on Widget, see the guide article on widgets.

公共属性

隐藏继承的属性

属性类型描述定义在
$autoIdPrefix string The prefix to the automatically generated widget IDs. yii\base\Widget
$behaviors yii\base\Behavior[] 附加到此组件的行为列表. yii\base\Component
$counter integer A counter used to generate $id for widgets. yii\base\Widget
$id string ID of the widget. yii\base\Widget
$stack yii\base\Widget[] The widgets that are currently being rendered (not ended). yii\base\Widget
$view yii\web\View The view object that can be used to render views or view files. yii\base\Widget
$viewPath string The directory containing the view files for this widget. yii\base\Widget

公共方法

隐藏继承的方法

方法描述定义在
__call() 调用不是类方法的指定的方法. yii\base\Component
__clone() 在通过克隆现有对象创建对象之后调用此方法. yii\base\Component
__construct() 构造函数. yii\base\BaseObject
__get() 返回组件属性的值. yii\base\Component
__isset() 检查属性是否已设置, 即已定义且不为null. yii\base\Component
__set() 设置组件属性的值. yii\base\Component
__unset() 将组件属性设置为null. yii\base\Component
afterRun() This method is invoked right after a widget is executed. yii\base\Widget
attachBehavior() 将行为附加到此组件. yii\base\Component
attachBehaviors() 将行为列表附加到组件. yii\base\Component
beforeRun() This method is invoked right before the widget is executed. yii\base\Widget
begin() Begins a widget. yii\base\Widget
behaviors() 返回此组件应该具有的行为列表. yii\base\Component
canGetProperty() 返回一个值, 指示属性是否可读取. yii\base\Component
canSetProperty() 返回一个值, 指示属性是否可设置. yii\base\Component
className() 返回此类的完全限定名称. yii\base\BaseObject
detachBehavior() 从组件中分离一个行为. yii\base\Component
detachBehaviors() 分离组件中的所有行为. yii\base\Component
end() Ends a widget. yii\base\Widget
ensureBehaviors() 确保在 behaviors() 中声明的行为已附加到此组件. yii\base\Component
getBehavior() 返回指定的行为对象. yii\base\Component
getBehaviors() 返回附加到此组件的所有行为. yii\base\Component
getId() Returns the ID of the widget. yii\base\Widget
getView() Returns the view object that can be used to render views or view files. yii\base\Widget
getViewPath() Returns the directory containing the view files for this widget. yii\base\Widget
hasEventHandlers() 返回一个值, 指示是否有任何处理程序附加到指定的事件. yii\base\Component
hasMethod() 返回一个值, 指示方法是否已定义. yii\base\Component
hasProperty() 返回一个值, 指示属性是否已定义. yii\base\Component
init() Initializes the object. yii\base\Widget
off() 从该组件分离现有的事件处理程序. yii\base\Component
on() 将事件处理程序附加到事件. yii\base\Component
render() Renders a view. yii\base\Widget
renderFile() Renders a view file. yii\base\Widget
run() Executes the widget. yii\base\Widget
setId() Sets the ID of the widget. yii\base\Widget
setView() Sets the view object to be used by this widget. yii\base\Widget
trigger() 触发事件. yii\base\Component
widget() Creates a widget instance and runs it. yii\base\Widget

事件

隐藏继承的事件

事件类型描述定义在
EVENT_AFTER_RUN yii\base\WidgetEvent An event raised right after executing a widget. (available since version 2.0.11) yii\base\Widget
EVENT_BEFORE_RUN yii\base\WidgetEvent An event raised right before executing a widget. (available since version 2.0.11) yii\base\Widget
EVENT_INIT yii\base\Event An event that is triggered when the widget is initialized via init(). (available since version 2.0.11) yii\base\Widget

属性详情

$autoIdPrefix public static 属性

The prefix to the automatically generated widget IDs.

参见 getId().

public static string $autoIdPrefix 'w'
$counter public static 属性

A counter used to generate $id for widgets.

public static integer $counter 0
$id public 属性

ID of the widget.

public string getId ( $autoGenerate true )
public void setId ( $value )
$stack public static 属性

The widgets that are currently being rendered (not ended). This property is maintained by begin() and end() methods.

public static yii\base\Widget[] $stack = []
$view public 属性

The view object that can be used to render views or view files.

public yii\web\View getView ( )
public void setView ( $view )
$viewPath public 只读 属性

The directory containing the view files for this widget.

public string getViewPath ( )

方法详情

afterRun() public 方法 (可用自版本: 2.0.11)

This method is invoked right after a widget is executed.

The method will trigger the EVENT_AFTER_RUN event. The return value of the method will be used as the widget return value.

If you override this method, your code should look like the following:

public function afterRun($result)
{
    $result = parent::afterRun($result);
    // your custom code here
    return $result;
}
public mixed afterRun ( $result )
$result mixed

The widget return result.

return mixed

The processed widget result.

beforeRun() public 方法 (可用自版本: 2.0.11)

This method is invoked right before the widget is executed.

The method will trigger the EVENT_BEFORE_RUN event. The return value of the method will determine whether the widget should continue to run.

When overriding this method, make sure you call the parent implementation like the following:

public function beforeRun()
{
    if (!parent::beforeRun()) {
        return false;
    }

    // your custom code here

    return true; // or false to not run the widget
}
public boolean beforeRun ( )
return boolean

Whether the widget should continue to be executed.

begin() public static 方法

Begins a widget.

This method creates an instance of the calling class. It will apply the configuration to the created instance. A matching end() call should be called later. As some widgets may use output buffering, the end() call should be made in the same view to avoid breaking the nesting of output buffers.

参见 end().

public static static begin ( $config = [] )
$config array

Name-value pairs that will be used to initialize the object properties

return static

The newly created widget instance

end() public static 方法

Ends a widget.

Note that the rendering result of the widget is directly echoed out.

参见 begin().

public static static end ( )
return static

The widget instance that is ended.

throws yii\base\InvalidCallException

if begin() and end() calls are not properly nested

getId() public 方法

Returns the ID of the widget.

public string getId ( $autoGenerate true )
$autoGenerate boolean

Whether to generate an ID if it is not set previously

return string

ID of the widget.

getView() public 方法

Returns the view object that can be used to render views or view files.

The render() and renderFile() methods will use this view object to implement the actual view rendering. If not set, it will default to the "view" application component.

public yii\web\View getView ( )
return yii\web\View

The view object that can be used to render views or view files.

getViewPath() public 方法

Returns the directory containing the view files for this widget.

The default implementation returns the 'views' subdirectory under the directory containing the widget class file.

public string getViewPath ( )
return string

The directory containing the view files for this widget.

init() public 方法

Initializes the object.

This method is called at the end of the constructor. The default implementation will trigger an EVENT_INIT event.

public void init ( )
render() public 方法

Renders a view.

The view to be rendered can be specified in one of the following formats:

  • path alias (e.g. "@app/views/site/index");
  • absolute path within application (e.g. "//site/index"): the view name starts with double slashes. The actual view file will be looked for under the view path of the application.
  • absolute path within module (e.g. "/site/index"): the view name starts with a single slash. The actual view file will be looked for under the view path of the currently active module.
  • relative path (e.g. "index"): the actual view file will be looked for under $viewPath.

If the view name does not contain a file extension, it will use the default one .php.

public string render ( $view, $params = [] )
$view string

The view name.

$params array

The parameters (name-value pairs) that should be made available in the view.

return string

The rendering result.

throws yii\base\InvalidArgumentException

if the view file does not exist.

renderFile() public 方法

Renders a view file.

public string renderFile ( $file, $params = [] )
$file string

The view file to be rendered. This can be either a file path or a path alias.

$params array

The parameters (name-value pairs) that should be made available in the view.

return string

The rendering result.

throws yii\base\InvalidArgumentException

if the view file does not exist.

run() public 方法

Executes the widget.

public string run ( )
return string

The result of widget execution to be outputted.

setId() public 方法

Sets the ID of the widget.

public void setId ( $value )
$value string

Id of the widget.

setView() public 方法

Sets the view object to be used by this widget.

public void setView ( $view )
$view yii\base\View

The view object that can be used to render views or view files.

widget() public static 方法

Creates a widget instance and runs it.

The widget rendering result is returned by this method.

public static string widget ( $config = [] )
$config array

Name-value pairs that will be used to initialize the object properties

return string

The rendering result of the widget.

throws Exception

事件详情

EVENT_AFTER_RUN 事件类型 yii\base\WidgetEvent (可用自版本: 2.0.11)

An event raised right after executing a widget.

EVENT_BEFORE_RUN 事件类型 yii\base\WidgetEvent (可用自版本: 2.0.11)

An event raised right before executing a widget. You may set yii\base\WidgetEvent::$isValid to be false to cancel the widget execution.

EVENT_INIT 事件类型 yii\base\Event (可用自版本: 2.0.11)

An event that is triggered when the widget is initialized via init().