Widget
API reference › yii › base › Widget
16 subclasses
yii\console\widgets\Tableyii\widgets\ActiveFormyii\widgets\BaseListViewyii\widgets\Blockyii\widgets\Breadcrumbsyii\widgets\ContentDecoratoryii\widgets\DetailViewyii\widgets\FragmentCacheyii\widgets\InputWidgetyii\widgets\LinkPageryii\widgets\LinkSorteryii\widgets\Menuyii\widgets\Pjaxyii\widgets\Spacelessyii2\extensions\bootstrap6\Widgetyii\debug\widgets\FilterBanner
Widget is the base class for widgets.
For more details and usage information on Widget, see the guide article on widgets.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$autoIdPrefix static | string | The prefix to the automatically generated widget IDs. Default: 'w'. | Widget |
$behaviors read-only | array<Behavior<static>> | List of behaviors attached to this component. | Component |
$id | string | Id of the widget. | Widget |
$view | yii\ | The view object that can be used to render views or view files. | Widget |
$viewPath read-only | string | The directory containing the view files for this widget. | Widget |
Methods
Section titled “Methods”| Method | Description | Defined 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() static | Begins 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() static | Returns 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() static | Ends 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() static | Creates a widget instance and runs it. | Widget |
Events
Section titled “Events”| Event | Type | Description | Defined by |
|---|---|---|---|
EVENT_INIT | Event | An event that is triggered when the widget is initialized via init(). Event name: 'init'. Available since 2.0.11. | Widget |
EVENT_BEFORE_RUN | WidgetEvent | An event raised right before executing a widget. Event name: 'beforeRun'. Available since 2.0.11. | Widget |
EVENT_AFTER_RUN | WidgetEvent | An event raised right after executing a widget. Event name: 'afterRun'. Available since 2.0.11. | Widget |
Method details
Section titled “Method details”afterRun()
Section titled “afterRun() ”public function afterRun(mixed $result): mixedThis 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
| Parameter | Type | Description |
|---|---|---|
$result | mixed | The widget return result. |
Returns
mixed: The processed widget result.
Since
2.0.11
beforeRun()
Section titled “beforeRun() ”public function beforeRun(): boolThis 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
begin()
Section titled “begin() ”public static function begin(array $config = []): staticBegins 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
| Parameter | Type | Description |
|---|---|---|
$config | array | Name-value pairs that will be used to initialize the object properties |
Returns
static: The newly created widget instance
See also
end()
Section titled “end() ”public static function end(): staticEnds a widget.
Note that the rendering result of the widget is directly echoed out.
Returns
static: The widget instance that is ended.
Throws
InvalidCallException: ifbegin()andend()calls are not properly nested
See also
getId()
Section titled “getId() ”public function getId(bool $autoGenerate = true): string|nullReturns the ID of the widget.
Parameters
| Parameter | Type | Description |
|---|---|---|
$autoGenerate | bool | Whether to generate an ID if it is not set previously |
Returns
string|null: ID of the widget.
getView()
Section titled “getView() ”public function getView(): yii\web\ViewReturns 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.
getViewPath()
Section titled “getViewPath() ”public function getViewPath(): stringReturns 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.
init()
Section titled “init() ”public function init(): mixedInitializes the object.
This method is called at the end of the constructor.
The default implementation will trigger an EVENT_INIT event.
Returns
mixed
render()
Section titled “render() ”public function render(string $view, array $params = []): stringRenders 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
| Parameter | Type | Description |
|---|---|---|
$view | string | The view name. |
$params | array | The parameters (name-value pairs) that should be made available in the view. |
Returns
string: The rendering result.
Throws
InvalidArgumentException: if the view file does not exist.
renderFile()
Section titled “renderFile() ”public function renderFile(string $file, array $params = []): stringRenders a view file.
Parameters
| Parameter | Type | Description |
|---|---|---|
$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. |
Returns
string: The rendering result.
Throws
InvalidArgumentException: if the view file does not exist.
run()
Section titled “run() ”public function run(): string|voidExecutes the widget.
Returns
string|void: The rendering result may be directly “echoed” or returned as a string
setId()
Section titled “setId() ”public function setId(string $value): mixedSets the ID of the widget.
Parameters
| Parameter | Type | Description |
|---|---|---|
$value | string | Id of the widget. |
Returns
mixed
setView()
Section titled “setView() ”public function setView(yii\web\View $view): mixedSets the view object to be used by this widget.
Parameters
| Parameter | Type | Description |
|---|---|---|
$view | yii\ | The view object that can be used to render views or view files. |
Returns
mixed
widget()
Section titled “widget() ”public static function widget(array $config = []): stringCreates a widget instance and runs it.
The widget rendering result is returned by this method.
Parameters
| Parameter | Type | Description |
|---|---|---|
$config | array | Name-value pairs that will be used to initialize the object properties |
Returns
string: The rendering result of the widget.
Throws