Inheritance | yii\widgets\Menu » yii\base\Widget » yii\base\Component » yii\base\Object |
---|---|
Implements | yii\base\Configurable, yii\base\ViewContextInterface |
Subclasses | yii\jui\Menu |
Available since version | 2.0 |
Source Code | https://github.com/yiisoft/yii2/blob/master/framework/widgets/Menu.php |
Menu displays a multi-level menu using nested HTML lists.
The main property of Menu is $items, which specifies the possible items in the menu. A menu item can contain sub-items which specify the sub-menu under that menu item.
Menu checks the current route and request parameters to toggle certain menu items with active state.
Note that Menu only renders the HTML tags about the menu. It does do any styling. You are responsible to provide CSS styles to make it look like a real menu.
The following example shows how to use Menu:
echo Menu::widget([ 'items' => [ // Important: you need to specify url as 'controller/action', // not just as 'controller' even if default action is used. ['label' => 'Home', 'url' => ['site/index']], // 'Products' menu item will be selected as long as the route is 'product/index' ['label' => 'Products', 'url' => ['product/index'], 'items' => [ ['label' => 'New Arrivals', 'url' => ['product/index', 'tag' => 'new']], ['label' => 'Most Popular', 'url' => ['product/index', 'tag' => 'popular']], ]], ['label' => 'Login', 'url' => ['site/login'], 'visible' => Yii::$app->user->isGuest], ], ]);
Property | Type | Description | Defined By |
---|---|---|---|
$activateItems | boolean | Whether to automatically activate items according to whether their route setting matches the currently requested route. | yii\widgets\Menu |
$activateParents | boolean | Whether to activate parent menu items when one of the corresponding child menu items is active. | yii\widgets\Menu |
$activeCssClass | string | The CSS class to be appended to the active menu item. | yii\widgets\Menu |
$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 |
$encodeLabels | boolean | Whether the labels for menu items should be HTML-encoded. | yii\widgets\Menu |
$firstItemCssClass | string | The CSS class that will be assigned to the first item in the main menu or each submenu. | yii\widgets\Menu |
$hideEmptyItems | boolean | Whether to hide empty menu items. | yii\widgets\Menu |
$id | string | ID of the widget. | yii\base\Widget |
$itemOptions | array | List of HTML attributes shared by all menu $items. | yii\widgets\Menu |
$items | array | List of menu items. | yii\widgets\Menu |
$labelTemplate | string | The template used to render the body of a menu which is NOT a link. | yii\widgets\Menu |
$lastItemCssClass | string | The CSS class that will be assigned to the last item in the main menu or each submenu. | yii\widgets\Menu |
$linkTemplate | string | The template used to render the body of a menu which is a link. | yii\widgets\Menu |
$options | array | The HTML attributes for the menu's container tag. | yii\widgets\Menu |
$params | array | The parameters used to determine if a menu item is active or not. | yii\widgets\Menu |
$route | string | The route used to determine if a menu item is active or not. | yii\widgets\Menu |
$stack | yii\base\Widget[] | The widgets that are currently being rendered (not ended). | yii\base\Widget |
$submenuTemplate | string | The template used to render a list of sub-menus. | yii\widgets\Menu |
$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 |
Method | Description | Defined 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 |
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\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\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() | Renders the menu. | yii\widgets\Menu |
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 |
Method | Description | Defined By |
---|---|---|
isItemActive() | Checks whether a menu item is active. | yii\widgets\Menu |
normalizeItems() | Normalizes the $items property to remove invisible items and activate certain items. | yii\widgets\Menu |
renderItem() | Renders the content of a menu item. | yii\widgets\Menu |
renderItems() | Recursively renders the menu items (without the container tag). | yii\widgets\Menu |
Event | Type | Description | Defined By |
---|---|---|---|
EVENT_AFTER_RUN | yii\base\WidgetEvent | An event raised right after executing a widget. (available since version 2.0.11) | yii\base\Widget |
EVENT_BEFORE_RUN | yii\base\WidgetEvent | An event raised right before executing a widget. (available since version 2.0.11) | yii\base\Widget |
EVENT_INIT | yii\base\Event | An event that is triggered when the widget is initialized via init(). (available since version 2.0.11) | yii\base\Widget |
Whether to automatically activate items according to whether their route setting matches the currently requested route.
See also isItemActive().
public boolean $activateItems = true
Whether to activate parent menu items when one of the corresponding child menu items is active. The activated parent menu items will also have its CSS classes appended with $activeCssClass.
public boolean $activateParents = false
The CSS class to be appended to the active menu item.
public string $activeCssClass = 'active'
Whether the labels for menu items should be HTML-encoded.
public boolean $encodeLabels = true
The CSS class that will be assigned to the first item in the main menu or each submenu. Defaults to null, meaning no such CSS class will be assigned.
public string $firstItemCssClass = null
Whether to hide empty menu items. An empty menu item is one whose url
option is not set and which has no visible child menu items.
public boolean $hideEmptyItems = true
List of HTML attributes shared by all menu $items. If any individual menu item specifies its options
, it will be merged with this property before being used to generate the HTML attributes for the menu item tag. The following special options are recognized:
See also yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
public array $itemOptions = []
List of menu items. Each menu item should be an array of the following structure:
function ($item, $hasActiveChild, $isItemActive, $widget)
. Closure must return true
if item should be marked as active
, otherwise - false
. If a menu item is active, its CSS class will be appended with $activeCssClass. If this option is not set, the menu item will be set active automatically when the current request is triggered by url
. For more details, please refer to isItemActive().{url}
will be replaced by the URL associated with this menu item, and the token {label}
will be replaced by the label of the menu item. If this option is not set, $linkTemplate or $labelTemplate will be used instead.{items}
will be replaced with the rendered sub-menu items. If this option is not set, $submenuTemplate will be used instead.public array $items = []
The template used to render the body of a menu which is NOT a link. In this template, the token {label}
will be replaced with the label of the menu item. This property will be overridden by the template
option set in individual menu items via $items.
public string $labelTemplate = '{label}'
The CSS class that will be assigned to the last item in the main menu or each submenu. Defaults to null, meaning no such CSS class will be assigned.
public string $lastItemCssClass = null
The template used to render the body of a menu which is a link. In this template, the token {url}
will be replaced with the corresponding link URL; while {label}
will be replaced with the link text. This property will be overridden by the template
option set in individual menu items via $items.
public string $linkTemplate = '<a href="{url}">{label}</a>'
The HTML attributes for the menu's container tag. The following special options are recognized:
See also yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
public array $options = []
The parameters used to determine if a menu item is active or not. If not set, it will use $_GET
.
See also:
public array $params = null
The route used to determine if a menu item is active or not. If not set, it will use the route of the current request.
See also:
public string $route = null
The template used to render a list of sub-menus. In this template, the token {items}
will be replaced with the rendered sub-menu items.
public string $submenuTemplate = "\n<ul>\n{items}\n</ul>\n"
Checks whether a menu item is active.
This is done by checking if $route and $params match that specified in the url
option of the menu item. When the url
option of a menu item is specified in terms of an array, its first element is treated as the route for the item and the rest of the elements are the associated parameters. Only when its route and parameters match $route and $params, respectively, will a menu item be considered active.
protected boolean isItemActive ( $item ) | ||
---|---|---|
$item | array |
The menu item to be checked |
return | boolean |
Whether the menu item is active |
Normalizes the $items property to remove invisible items and activate certain items.
protected array normalizeItems ( $items, &$active ) | ||
---|---|---|
$items | array |
The items to be normalized. |
$active | boolean |
Whether there is an active child menu item. |
return | array |
The normalized menu items |
Renders the content of a menu item.
Note that the container and the sub-menus are not rendered here.
protected string renderItem ( $item ) | ||
---|---|---|
$item | array |
The menu item to be rendered. Please refer to $items to see what data might be in the item. |
return | string |
The rendering result |
Recursively renders the menu items (without the container tag).
protected string renderItems ( $items ) | ||
---|---|---|
$items | array |
The menu items to be rendered recursively |
return | string |
The rendering result |
Renders the menu.
public void run ( ) |
---|
© 2008–2017 by Yii Software LLC
Licensed under the three clause BSD license.
http://www.yiiframework.com/doc-2.0/yii-widgets-menu.html