继承yii\base\Widget » yii\base\Component » yii\base\Object
实现yii\base\ViewContextInterface
可用自版本2.0

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
$counter integer A counter used to generate \yii\base\id for widgets. yii\base\Widget
$stack yii\base\Widget[] The widgets that are currently being rendered (not ended). yii\base\Widget

公共方法

隐藏继承方法

方法描述定义在
afterRun() This method is invoked right after a widget is executed. yii\base\Widget
beforeRun() This method is invoked right before the widget is executed. yii\base\Widget
begin() Begins a widget. yii\base\Widget
end() Ends a widget. yii\base\Widget
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
init() Initializes the object. yii\base\Widget
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
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. (可用自版本 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 public static property

The prefix to the automatically generated widget IDs.

参见 getId().

public static string $autoIdPrefix 'w'
$counter public static property

A counter used to generate \yii\base\id for widgets.

public static integer $counter 0
$stack public static property

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

public static yii\base\Widget[] $stack = []

方法详情

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 yii\base\Application::viewPath 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 yii\base\Module::viewPath of the currently active module.
  • relative path (e.g. "index"): the actual view file will be looked for under \yii\base\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\InvalidParamException

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\InvalidParamException

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 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().