Class yii\base\Widget

Inheritanceyii\base\Widget » yii\base\Component » yii\base\Object
Implementsyii\base\Configurable, yii\base\ViewContextInterface
Subclassesyii\apidoc\templates\bootstrap\SideNavWidget, yii\authclient\widgets\AuthChoice, yii\authclient\widgets\AuthChoiceItem, yii\authclient\widgets\GooglePlusButton, yii\bootstrap\ActiveForm, yii\bootstrap\Alert, yii\bootstrap\Button, yii\bootstrap\ButtonDropdown, yii\bootstrap\ButtonGroup, yii\bootstrap\Carousel, yii\bootstrap\Collapse, yii\bootstrap\Dropdown, yii\bootstrap\Modal, yii\bootstrap\Nav, yii\bootstrap\NavBar, yii\bootstrap\Progress, yii\bootstrap\Tabs, yii\bootstrap\Widget, yii\captcha\Captcha, yii\grid\GridView, yii\jui\Accordion, yii\jui\AutoComplete, yii\jui\DatePicker, yii\jui\Dialog, yii\jui\Draggable, yii\jui\Droppable, yii\jui\InputWidget, yii\jui\Menu, yii\jui\ProgressBar, yii\jui\Resizable, yii\jui\Selectable, yii\jui\Slider, yii\jui\SliderInput, yii\jui\Sortable, yii\jui\Spinner, yii\jui\Tabs, yii\jui\Widget, 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
Available since version2.0
Source Code https://github.com/yiisoft/yii2/blob/master/framework/base/Widget.php

Widget is the base class for widgets.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$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

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. yii\base\Object
__clone() This method is called after the object is created by cloning an existing one. yii\base\Component
__construct() Constructor. yii\base\Object
__get() Returns the value of an object property. yii\base\Object
__isset() Checks if a property is set, i.e. defined and not null. yii\base\Object
__set() Sets value of an object property. yii\base\Object
__unset() Sets an object property to null. yii\base\Object
attachBehavior() Attaches a behavior to this component. yii\base\Component
attachBehaviors() Attaches a list of behaviors to the component. yii\base\Component
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\Object
canSetProperty() Returns a value indicating whether a property can be set. yii\base\Object
className() Returns the fully qualified name of this class. yii\base\Object
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\Object
hasProperty() Returns a value indicating whether a property is defined. yii\base\Object
init() Initializes the object. yii\base\Object
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

Property Details

$autoIdPrefix public property

The prefix to the automatically generated widget IDs.

See also getId().

$counter public property

A counter used to generate $id for widgets.

$id public property
string getId$autoGenerate true )
void setId$value )

ID of the widget.

$stack public property

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

$view public property
yii\web\View getView( )
void setView$view )

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

$viewPath public read-only property

The directory containing the view files for this widget.

Method Details

begin() public method

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.

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 method

Ends a widget.

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

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 method

Returns the ID of the widget.

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 method

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.

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

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

getViewPath() public method

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.

string getViewPath( )
return string

The directory containing the view files for this widget.

render() public method

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.

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 method

Renders a view file.

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 method

Executes the widget.

string run( )
return string

The result of widget execution to be outputted.

setId() public method

Sets the ID of the widget.

void setId$value )
$value string

Id of the widget.

setView() public method

Sets the view object to be used by this widget.

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

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

widget() public method

Creates a widget instance and runs it.

The widget rendering result is returned by this method.

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