Skip to content
PHP Ecosystem
Explore

Widget

API reference › yii › base › Widget

classyii\base\Widget
ExtendsComponent » BaseObject
ImplementsViewContextInterface, Configurable
Since2.0
Sourceframework/base/Widget.php
16 subclasses

Widget is the base class for widgets.

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

PropertyTypeDescriptionDefined by
$autoIdPrefix staticstringThe prefix to the automatically generated widget IDs. Default: 'w'.Widget
$behaviors read-onlyarray<Behavior<static>>List of behaviors attached to this component.Component
$idstringId of the widget.Widget
$viewyii\web\ViewThe view object that can be used to render views or view files.Widget
$viewPath read-onlystringThe directory containing the view files for this widget.Widget
MethodDescriptionDefined by
__construct()Constructor.BaseObject
__call()Calls the named method which is not a class method.Component
__clone()This method is called after the object is created by cloning an existing one.Component
__get()Returns the value of a component property.Component
__isset()Checks if a property is set, i.e. defined and not null.Component
__set()Sets the value of a component property.Component
__unset()Sets a component property to be null.Component
afterRun()This method is invoked right after a widget is executed.Widget
attachBehavior()Attaches a behavior to this component.Component
attachBehaviors()Attaches a list of behaviors to the component.Component
beforeRun()This method is invoked right before the widget is executed.Widget
begin() staticBegins a widget.Widget
behaviors()Returns a list of behaviors that this component should behave as.Component
canGetProperty()Returns a value indicating whether a property can be read.Component
canSetProperty()Returns a value indicating whether a property can be set.Component
className() staticReturns the fully qualified name of this class.BaseObject
detachBehavior()Detaches a behavior from the component.Component
detachBehaviors()Detaches all behaviors from the component.Component
end() staticEnds a widget.Widget
ensureBehaviors()Makes sure that the behaviors declared in behaviors() are attached to this component.Component
getBehavior()Returns the named behavior object.Component
getBehaviors()Returns all behaviors attached to this component.Component
getId()Returns the ID of the widget.Widget
getView()Returns the view object that can be used to render views or view files.Widget
getViewPath()Returns the directory containing the view files for this widget.Widget
hasEventHandlers()Returns a value indicating whether there is any handler attached to the named event.Component
hasMethod()Returns a value indicating whether a method is defined.Component
hasProperty()Returns a value indicating whether a property is defined for this component.Component
init()Initializes the object.Widget
off()Detaches an existing event handler from this component.Component
on()Attaches an event handler to an event.Component
render()Renders a view.Widget
renderFile()Renders a view file.Widget
run()Executes the widget.Widget
setId()Sets the ID of the widget.Widget
setView()Sets the view object to be used by this widget.Widget
trigger()Triggers an event.Component
widget() staticCreates a widget instance and runs it.Widget
EventTypeDescriptionDefined by
EVENT_INITEventAn event that is triggered when the widget is initialized via init(). Event name: 'init'. Available since 2.0.11.Widget
EVENT_BEFORE_RUNWidgetEventAn event raised right before executing a widget. Event name: 'beforeRun'. Available since 2.0.11.Widget
EVENT_AFTER_RUNWidgetEventAn event raised right after executing a widget. Event name: 'afterRun'. Available since 2.0.11.Widget
public function afterRun(mixed $result): mixed

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;
}

Parameters

ParameterTypeDescription
$resultmixedThe widget return result.

Returns

mixed: The processed widget result.

Since

2.0.11

public function beforeRun(): bool

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
}

Returns

bool: Whether the widget should continue to be executed.

Since

2.0.11

public static function begin(array $config = []): 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.

Parameters

ParameterTypeDescription
$configarrayName-value pairs that will be used to initialize the object properties

Returns

static: The newly created widget instance

See also

public static function end(): static

Ends a widget.

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

Returns

static: The widget instance that is ended.

Throws

See also

public function getId(bool $autoGenerate = true): string|null

Returns the ID of the widget.

Parameters

ParameterTypeDescription
$autoGenerateboolWhether to generate an ID if it is not set previously

Returns

string|null: ID of the widget.

public function getView(): yii\web\View

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.

Returns

yii\web\View: The view object that can be used to render views or view files.

public function getViewPath(): string

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.

Returns

string: The directory containing the view files for this widget.

public function init(): mixed

Initializes the object.

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

Returns

mixed

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

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.

Parameters

ParameterTypeDescription
$viewstringThe view name.
$paramsarrayThe parameters (name-value pairs) that should be made available in the view.

Returns

string: The rendering result.

Throws

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

Renders a view file.

Parameters

ParameterTypeDescription
$filestringThe view file to be rendered. This can be either a file path or a path alias.
$paramsarrayThe parameters (name-value pairs) that should be made available in the view.

Returns

string: The rendering result.

Throws

public function run(): string|void

Executes the widget.

Returns

string|void: The rendering result may be directly “echoed” or returned as a string

public function setId(string $value): mixed

Sets the ID of the widget.

Parameters

ParameterTypeDescription
$valuestringId of the widget.

Returns

mixed

public function setView(yii\web\View $view): mixed

Sets the view object to be used by this widget.

Parameters

ParameterTypeDescription
$viewyii\web\ViewThe view object that can be used to render views or view files.

Returns

mixed

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

Creates a widget instance and runs it.

The widget rendering result is returned by this method.

Parameters

ParameterTypeDescription
$configarrayName-value pairs that will be used to initialize the object properties

Returns

string: The rendering result of the widget.

Throws