Class yii\widgets\Menu
Inheritance | yii\widgets\Menu » yii\base\Widget » yii\base\Component » yii\base\BaseObject |
---|---|
Implements | yii\base\Configurable, yii\base\ViewContextInterface |
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],
],
]);
Public Properties
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 |
Public Methods
Method | Description | Defined By |
---|---|---|
__call() | Calls the named method which is not a class method. | yii\base\Component |
__clone() | This method is called after the object is created by cloning an existing one. | yii\base\Component |
__construct() | Constructor. | yii\base\BaseObject |
__get() | Returns the value of a component property. | yii\base\Component |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\base\Component |
__set() | Sets the value of a component property. | yii\base\Component |
__unset() | Sets a component property to be null. | yii\base\Component |
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\Component |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\base\Component |
className() | Returns the fully qualified name of this class. | yii\base\BaseObject |
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\Component |
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\base\Component |
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 |
Protected Methods
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 |
Events
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 |
Property Details
Whether to automatically activate items according to whether their route setting matches the currently requested route.
See also isItemActive().
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.
The CSS class to be appended to the active menu item.
Whether the labels for menu items should be HTML-encoded.
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.
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.
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.
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 item`s 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 yii\helpers\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 or Closure, optional, whether this menu item is in active state (currently selected).
When using a closure, its signature should be
function ($item, $hasActiveChild, $isItemActive, $widget)
. Closure must returntrue
if item should be marked asactive
, 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 byurl
. For more details, please refer to 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.
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.
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.
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.
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.
The parameters used to determine if a menu item is active or not.
If not set, it will use $_GET
.
See also:
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:
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.
Method Details
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 ( ) |
Signup or Login in order to comment.