PHP Class yii\widgets\Menu

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: php 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], ], ]);
Since: 2.0
Author: Qiang Xue ([email protected])
Inheritance: extends yii\base\Widget
Mostra file Open project: yiisoft/yii2 Class Usage Examples

Public Properties

Property Type Description
$activateItems whether to automatically activate items according to whether their route setting matches the currently requested route.
$activateParents 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]].
$activeCssClass the CSS class to be appended to the active menu item.
$encodeLabels whether the labels for menu items should be HTML-encoded.
$firstItemCssClass 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.
$hideEmptyItems 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.
$itemOptions 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: - tag: string, defaults to "li", the tag name of the item container tags. Set to false to disable container tag. See also [[\yii\helpers\Html::tag()]].
$items list of menu items. Each menu item should be an array of the following structure: - label: string, optional, specifies the menu item label. When [[encodeLabels]] is true, the label will be HTML-encoded. If the label is not specified, an empty string will be used. - encode: boolean, optional, whether this items label should be HTML-encoded. This param will override global [[encodeLabels]] param. - url: string or array, optional, specifies the URL of the menu item. It will be processed by [[Url::to]]. When this is set, the actual menu item content will be generated using [[linkTemplate]]; otherwise, [[labelTemplate]] will be used. - visible: boolean, optional, whether this menu item is visible. Defaults to true. - items: array, optional, specifies the sub-menu items. Its format is the same as the parent items. - active: boolean, optional, whether this menu item is in active state (currently selected). 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 Menu::isItemActive. - template: string, optional, the template used to render the content of this menu item. The token {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. - submenuTemplate: string, optional, the template used to render the list of sub-menus. The token {items}` will be replaced with the rendered sub-menu items. If this option is not set, [[submenuTemplate]] will be used instead. - options: array, optional, the HTML attributes for the menu container tag.
$labelTemplate 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]].
$lastItemCssClass 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.
$linkTemplate 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]].
$options the HTML attributes for the menu's container tag. The following special options are recognized: - tag: string, defaults to "ul", the tag name of the item container tags. Set to false to disable container tag. See also [[\yii\helpers\Html::tag()]].
$params the parameters used to determine if a menu item is active or not. If not set, it will use $_GET.
$route 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.
$submenuTemplate 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 Methods

Method Description
run ( ) Renders the menu.

Protected Methods

Method Description
isItemActive ( array $item ) : boolean Checks whether a menu item is active.
normalizeItems ( array $items, boolean &$active ) : array Normalizes the [[items]] property to remove invisible items and activate certain items.
renderItem ( array $item ) : string Renders the content of a menu item.
renderItems ( array $items ) : string Recursively renders the menu items (without the container tag).

Method Details

isItemActive() protected method

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 isItemActive ( array $item ) : boolean
$item array the menu item to be checked
return boolean whether the menu item is active

normalizeItems() protected method

Normalizes the [[items]] property to remove invisible items and activate certain items.
protected normalizeItems ( array $items, boolean &$active ) : array
$items array the items to be normalized.
$active boolean whether there is an active child menu item.
return array the normalized menu items

renderItem() protected method

Note that the container and the sub-menus are not rendered here.
protected renderItem ( array $item ) : string
$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

renderItems() protected method

Recursively renders the menu items (without the container tag).
protected renderItems ( array $items ) : string
$items array the menu items to be rendered recursively
return string the rendering result

run() public method

Renders the menu.
public run ( )

Property Details

$activateItems public_oe property

whether to automatically activate items according to whether their route setting matches the currently requested route.
See also: isItemActive()
public $activateItems

$activateParents public_oe property

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 $activateParents

$activeCssClass public_oe property

the CSS class to be appended to the active menu item.
public $activeCssClass

$encodeLabels public_oe property

whether the labels for menu items should be HTML-encoded.
public $encodeLabels

$firstItemCssClass public_oe property

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 $firstItemCssClass

$hideEmptyItems public_oe property

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 $hideEmptyItems

$itemOptions public_oe property

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: - tag: string, defaults to "li", the tag name of the item container tags. Set to false to disable container tag. See also [[\yii\helpers\Html::tag()]].
See also: yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
public $itemOptions

$items public_oe property

list of menu items. Each menu item should be an array of the following structure: - label: string, optional, specifies the menu item label. When [[encodeLabels]] is true, the label will be HTML-encoded. If the label is not specified, an empty string will be used. - encode: boolean, optional, whether this items label should be HTML-encoded. This param will override global [[encodeLabels]] param. - url: string or array, optional, specifies the URL of the menu item. It will be processed by [[Url::to]]. When this is set, the actual menu item content will be generated using [[linkTemplate]]; otherwise, [[labelTemplate]] will be used. - visible: boolean, optional, whether this menu item is visible. Defaults to true. - items: array, optional, specifies the sub-menu items. Its format is the same as the parent items. - active: boolean, optional, whether this menu item is in active state (currently selected). 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 Menu::isItemActive. - template: string, optional, the template used to render the content of this menu item. The token {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. - submenuTemplate: string, optional, the template used to render the list of sub-menus. The token {items}` will be replaced with the rendered sub-menu items. If this option is not set, [[submenuTemplate]] will be used instead. - options: array, optional, the HTML attributes for the menu container tag.
public $items

$labelTemplate public_oe property

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 $labelTemplate

$lastItemCssClass public_oe property

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 $lastItemCssClass

$linkTemplate public_oe property

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 $linkTemplate

$options public_oe property

the HTML attributes for the menu's container tag. The following special options are recognized: - tag: string, defaults to "ul", the tag name of the item container tags. Set to false to disable container tag. See also [[\yii\helpers\Html::tag()]].
See also: yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
public $options

$params public_oe property

the parameters used to determine if a menu item is active or not. If not set, it will use $_GET.
See also: route
See also: isItemActive()
public $params

$route public_oe property

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: params
See also: isItemActive()
public $route

$submenuTemplate public_oe property

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 $submenuTemplate