没有命名空间的类 yii yii\base yii\behaviors yii\caching yii\captcha yii\console yii\console\controllers yii\console\widgets yii\data yii\db yii\db\conditions yii\db\cubrid yii\db\cubrid\conditions yii\db\mssql yii\db\mssql\conditions yii\db\mysql yii\db\oci yii\db\oci\conditions yii\db\pgsql yii\db\sqlite yii\db\sqlite\conditions yii\di yii\filters yii\filters\auth yii\grid yii\helpers yii\i18n yii\log yii\mail yii\mutex yii\rbac yii\rest yii\test yii\validators yii\web yii\widgets

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/yiichina/yii2/blob/api/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[] List of behaviors attached to this component 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() Calls the named method which is not a class method. yii\base\Component
__clone() This method is called after the object is created by cloning an existing one. yii\base\Component
__construct() Constructor. yii\base\BaseObject
__get() Returns the value of a component property. yii\base\Component
__isset() Checks if a property is set, i.e. defined and not null. yii\base\Component
__set() Sets the value of a component property. yii\base\Component
__unset() Sets a component property to be null. yii\base\Component
afterRun() This method is invoked right after a widget is executed. yii\base\Widget
attachBehavior() Attaches a behavior to this component. yii\base\Component
attachBehaviors() Attaches a list of behaviors to the component. 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() Returns a list of behaviors that this component should behave as. yii\base\Component
canGetProperty() Returns a value indicating whether a property can be read. yii\base\Component
canSetProperty() Returns a value indicating whether a property can be set. yii\base\Component
className() Returns the fully qualified name of this class. yii\base\BaseObject
detachBehavior() Detaches a behavior from the component. yii\base\Component
detachBehaviors() Detaches all behaviors from the component. yii\base\Component
end() Ends a widget. yii\base\Widget
ensureBehaviors() Makes sure that the behaviors declared in behaviors() are attached to this component. yii\base\Component
getBehavior() Returns the named behavior object. yii\base\Component
getBehaviors() Returns all behaviors attached to this component. 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() Returns a value indicating whether there is any handler attached to the named event. yii\base\Component
hasMethod() Returns a value indicating whether a method is defined. yii\base\Component
hasProperty() Returns a value indicating whether a property is defined for this component. yii\base\Component
init() Initializes the object. yii\base\Widget
off() Detaches an existing event handler from this component. yii\base\Component
on() Attaches an event handler to an event. 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() Triggers an event. yii\base\Component
widget() Creates a widget instance and runs it. yii\base\Widget

Events

隐藏继承的事件

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

属性详情

$autoIdPrefix 公共 静态 属性

The prefix to the automatically generated widget IDs.

参见 getId().

public static string $autoIdPrefix 'w'
$counter 公共 静态 属性

A counter used to generate $id for widgets.

public static integer $counter 0
$id 公共 属性

ID of the widget.

public string getId ( $autoGenerate true )
public void setId ( $value )
$stack 公共 静态 属性

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 公共 属性

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

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

The directory containing the view files for this widget.

public string getViewPath ( )

方法详情

afterRun() 公共 方法 (自版本 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() 公共 方法 (自版本 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() 公共 静态 方法

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() 公共 静态 方法

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() 公共 方法

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() 公共 方法

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() 公共 方法

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() 公共 方法

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() 公共 方法

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() 公共 方法

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() 公共 方法

Executes the widget.

public string run()
return string

The result of widget execution to be outputted.

setId() 公共 方法

Sets the ID of the widget.

public void setId($value)
$value string

Id of the widget.

setView() 公共 方法

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() 公共 静态 方法

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 event of type yii\base\WidgetEvent ( 自版本 2.0.11 可用)

An event raised right after executing a widget.

EVENT_BEFORE_RUN event of type 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 event of type yii\base\Event ( 自版本 2.0.11 可用)

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