Nav
API reference › Bootstrap 6 › Nav
Renders a Bootstrap nav with links, active state detection by route, and items that toggle a Menu.
Menu toggles bind the Menu plugin through the data API; plugin events bubble up to the nav element, so
$clientEvents such as shown.bs.menu can be attached to it.
See also: https://getbootstrap.com/docs/6.0/components/nav/.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$activateItems | bool | Whether to activate items whose url route and parameters match $route and $params. Default: true. | Nav |
$activateParents | bool | Whether to activate a parent item when one of its menu items is active. Default: false. | Nav |
$autoIdPrefix static | string | The prefix to the automatically generated widget IDs. Default: 'w'. | yii\ |
$behaviors read-only | array<yii\<static>> | List of behaviors attached to this component. | yii\ |
$clientEvents | array<string, yii\| | Event handlers attached to the widget element, as event name => JavaScript handler source. Default: []. | BootstrapWidgetTrait |
$clientOptions | array<string, | Options passed to the plugin constructor, or false to skip the JavaScript initialization. Default: []. | BootstrapWidgetTrait |
$encodeLabels | bool | Whether to HTML-encode the item labels; the item encode option takes precedence. Default: true. | Nav |
$id | string | Id of the widget. | yii\ |
$items | array<array-key,yii2\| | Nav items: NavItem instances, arrays converted through NavItem::fromArray(), or strings rendered as is. Default: []. | Nav |
$layout | yii2\| | Layout class, such as NavLayout::FILL, or null for the default layout. Default: null. | Nav |
$menuClass | class-string<Menu> | Class of the menu widget. Default: \. | Nav |
$options | array<string, | HTML attributes of the widget container tag. Default: []. | Widget |
$params | array<array-key, | Query parameters used to detect active items, or null to use the current request parameters. Default: null. | Nav |
$route | string| | Route used to detect active items, or null to use the route of the current controller. Default: null. | Nav |
$style | yii2\| | Style class, such as NavStyle::TABS, or null for the base nav. Default: null. | Nav |
$view | yii\ | The view object that can be used to render views or view files. | yii\ |
$viewPath read-only | string | The directory containing the view files for this widget. | yii\ |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__call() | Calls the named method which is not a class method. | yii\ |
__clone() | This method is called after the object is created by cloning an existing one. | yii\ |
__get() | Returns the value of a component property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets the value of a component property. | yii\ |
__unset() | Sets a component property to be null. | yii\ |
addAttributes() | Adds a set of HTML attributes; attributes already set with the same name are replaced. | AttributesTrait |
addClass() | Adds one or more CSS classes to the existing classes; duplicated classes are collapsed. | AttributesTrait |
addCssStyle() | Adds CSS style properties to the style attribute. | AttributesTrait |
afterRun() | This method is invoked right after a widget is executed. | yii\ |
attachBehavior() | Attaches a behavior to this component. | yii\ |
attachBehaviors() | Attaches a list of behaviors to the component. | yii\ |
attribute() | Sets an HTML attribute value. | AttributesTrait |
beforeRun() | This method is invoked right before the widget is executed. | yii\ |
begin() static | Begins a widget. | yii\ |
behaviors() | Returns a list of behaviors that this component should behave as. | yii\ |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
class() | Replaces the existing CSS classes with the specified ones. | AttributesTrait |
className() static | Returns the fully qualified name of this class. | yii\ |
detachBehavior() | Detaches a behavior from the component. | yii\ |
detachBehaviors() | Detaches all behaviors from the component. | yii\ |
end() static | Ends a widget. | yii\ |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\ |
getBehavior() | Returns the named behavior object. | yii\ |
getBehaviors() | Returns all behaviors attached to this component. | yii\ |
getId() | Returns the ID of the widget. | yii\ |
getView() | Returns the view object that can be used to render views or view files. | yii\ |
getViewPath() | Returns the directory containing the view files for this widget. | yii\ |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\ |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\ |
init() | Initializes the widget, validates $layout and $style, and resolves $route and $params. | Nav |
off() | Detaches an existing event handler from this component. | yii\ |
on() | Attaches an event handler to an event. | yii\ |
render() | Renders a view. | yii\ |
renderFile() | Renders a view file. | yii\ |
run() | Nav | |
setId() | Sets the ID of the widget. | yii\ |
setView() | Sets the view object to be used by this widget. | yii\ |
styleClasses() static | Returns the classes of a nav style; NavStyle::PILLS_VERTICAL also needs NavStyle::PILLS. | Nav |
trigger() | Triggers an event. | yii\ |
widget() static | Creates a widget instance and runs it. | yii\ |
isChildActive() protected | Marks the active menu items and returns them. | Nav |
isItemActive() protected | Returns whether the item is active. | Nav |
pluginInitialization() protected | Returns the JavaScript that initializes a plugin with its normalized constructor options. | BootstrapWidgetTrait |
registerClientEvents() protected | Registers the JavaScript event handlers listed in $clientEvents at View::POS_END. | BootstrapWidgetTrait |
registerPlugin() protected | Registers the plugin asset, the plugin initialization when needed, and the $clientEvents. | BootstrapWidgetTrait |
renderItem() protected | Renders a nav item. | Nav |
renderMenu() protected | Renders the menu toggled by an item. | Nav |
Events
Section titled “Events”| Event | Type | Description | Defined by |
|---|---|---|---|
EVENT_INIT | yii\ | An event that is triggered when the widget is initialized via init(). Event name: 'init'. Available since 2.0.11. | yii\ |
EVENT_BEFORE_RUN | yii\ | An event raised right before executing a widget. Event name: 'beforeRun'. Available since 2.0.11. | yii\ |
EVENT_AFTER_RUN | yii\ | An event raised right after executing a widget. Event name: 'afterRun'. Available since 2.0.11. | yii\ |
Property details
Section titled “Property details”$items
Section titled “$items ”public array<array-key, array<array-key, mixed>|yii2\extensions\bootstrap6\item\NavItem|string> $items = []Nav items: NavItem instances, arrays converted through NavItem::fromArray(), or strings rendered
as is.
An array item accepts the following options:
label:string|Stringable, required.url:array|string|null, processed byUrl::to();nullrenders nohref. Defaults to#.active:bool, forces the active state; otherwise it is detected fromurl.disabled:bool, renders the link withouthrefand witharia-disabled="true".visible:bool, whether to render the item. Defaults totrue.encode:bool, whether to HTML-encode the label.linkOptions:array, HTML attributes of the.nav-linkelement.options:array, HTML attributes of the.nav-itemelement.items:array|string,Menu::$itemsof the menu toggled by the item, or the menu HTML.menuOptions:array, HTML attributes of the.menupanel.
$params
Section titled “$params ”public array<array-key, mixed>|null $params = nullQuery parameters used to detect active items, or null to use the current request parameters.
Scalar values are compared with the item url parameters as strings, so 42 matches '42'; arrays are
compared element by element.
Method details
Section titled “Method details”init()
Section titled “init() ”public function init(): voidInitializes the widget, validates $layout and $style, and resolves $route and
$params.
Throws
yii\base\InvalidConfigException: if$layoutor$styleis not a value of its enum.
run()
Section titled “run() ”public function run(): stringstyleClasses()
Section titled “styleClasses() ”public static function styleClasses(yii2\extensions\bootstrap6\enum\NavStyle|string|null $style): list<string>Returns the classes of a nav style; NavStyle::PILLS_VERTICAL also needs NavStyle::PILLS.
Parameters
| Parameter | Type | Description |
|---|---|---|
$style | yii2\| |
Returns
list<string>
Throws
yii\base\InvalidConfigException: if$styleis astringthat is not aNavStylevalue.
isChildActive()
Section titled “isChildActive() ”protected function isChildActive(array<array-key, yii2\extensions\bootstrap6\item\MenuItem|string> $items, bool &$active): array<array-key, yii2\extensions\bootstrap6\item\MenuItem|string>Marks the active menu items and returns them.
A menu item with active children is marked active as well. When $activateParents is true, $active is
set to true if any item is active.
Parameters
| Parameter | Type | Description |
|---|---|---|
$items | array<array-key, yii2\| | Menu items, see Menu::$items. |
$active | bool | Whether the parent item is active. |
Returns
array<array-key, yii2\extensions\bootstrap6\item\MenuItem|string>: Copies of the items, marked through MenuItem::withActive().
isItemActive()
Section titled “isItemActive() ”protected function isItemActive(yii2\extensions\bootstrap6\item\MenuItem|yii2\extensions\bootstrap6\item\NavItem $item): boolReturns whether the item is active.
The item active flag takes precedence. Otherwise, an item is active when the first element of its array url
is the route $route and the other elements match $params. Relative routes are resolved against the
module of the current controller.
Parameters are compared by value: scalars as strings, so ['site/users', 'id' => 42] matches the query string
id=42, and arrays element by element with the same keys. A null parameter matches any value.
Parameters
| Parameter | Type | Description |
|---|---|---|
$item | yii2\|yii2\ | Nav item, or item of a menu toggled by the nav. |
Returns
bool
renderItem()
Section titled “renderItem() ”protected function renderItem(yii2\extensions\bootstrap6\item\NavItem|string $item): stringRenders a nav item.
Parameters
| Parameter | Type | Description |
|---|---|---|
$item | yii2\| | Nav item, or a string rendered as is. |
Returns
string
renderMenu()
Section titled “renderMenu() ”protected function renderMenu(array<array-key, yii2\extensions\bootstrap6\item\MenuItem|string> $items, array<array-key, mixed> $options): stringRenders the menu toggled by an item.
Parameters
| Parameter | Type | Description |
|---|---|---|
$items | array<array-key, yii2\| | Menu items, see Menu::$items. |
$options | array<array-key, | HTML attributes of the .menu panel. |
Returns
string