Skip to content
PHP Ecosystem
Explore

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/.

PropertyTypeDescriptionDefined by
$activateItemsboolWhether to activate items whose url route and parameters match $route and $params. Default: true.Nav
$activateParentsboolWhether to activate a parent item when one of its menu items is active. Default: false.Nav
$autoIdPrefix staticstringThe prefix to the automatically generated widget IDs. Default: 'w'.yii\base\Widget
$behaviors read-onlyarray<yii\base\Behavior<static>>List of behaviors attached to this component.yii\base\Component
$clientEventsarray<string, yii\web\JsExpression|string>Event handlers attached to the widget element, as event name => JavaScript handler source. Default: [].BootstrapWidgetTrait
$clientOptionsarray<string, mixed>|falseOptions passed to the plugin constructor, or false to skip the JavaScript initialization. Default: [].BootstrapWidgetTrait
$encodeLabelsboolWhether to HTML-encode the item labels; the item encode option takes precedence. Default: true.Nav
$idstringId of the widget.yii\base\Widget
$itemsarray<array-key, array<array-key, mixed>|yii2\extensions\bootstrap6\item\NavItem|string>Nav items: NavItem instances, arrays converted through NavItem::fromArray(), or strings rendered as is. Default: [].Nav
$layoutyii2\extensions\bootstrap6\enum\NavLayout|string|nullLayout class, such as NavLayout::FILL, or null for the default layout. Default: null.Nav
$menuClassclass-string<Menu>Class of the menu widget. Default: \yii2\extensions\bootstrap6\Menu::class.Nav
$optionsarray<string, mixed>HTML attributes of the widget container tag. Default: [].Widget
$paramsarray<array-key, mixed>|nullQuery parameters used to detect active items, or null to use the current request parameters. Default: null.Nav
$routestring|nullRoute used to detect active items, or null to use the route of the current controller. Default: null.Nav
$styleyii2\extensions\bootstrap6\enum\NavStyle|string|nullStyle class, such as NavStyle::TABS, or null for the base nav. Default: null.Nav
$viewyii\web\ViewThe view object that can be used to render views or view files.yii\base\Widget
$viewPath read-onlystringThe directory containing the view files for this widget.yii\base\Widget
MethodDescriptionDefined by
__construct()Constructor.yii\base\BaseObject
__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
__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
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\base\Widget
attachBehavior()Attaches a behavior to this component.yii\base\Component
attachBehaviors()Attaches a list of behaviors to the component.yii\base\Component
attribute()Sets an HTML attribute value.AttributesTrait
beforeRun()This method is invoked right before the widget is executed.yii\base\Widget
begin() staticBegins 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
class()Replaces the existing CSS classes with the specified ones.AttributesTrait
className() staticReturns 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() staticEnds 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 widget, validates $layout and $style, and resolves $route and $params.Nav
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()Nav
setId()Sets the ID of the widget.yii\base\Widget
setView()Sets the view object to be used by this widget.yii\base\Widget
styleClasses() staticReturns the classes of a nav style; NavStyle::PILLS_VERTICAL also needs NavStyle::PILLS.Nav
trigger()Triggers an event.yii\base\Component
widget() staticCreates a widget instance and runs it.yii\base\Widget
isChildActive() protectedMarks the active menu items and returns them.Nav
isItemActive() protectedReturns whether the item is active.Nav
pluginInitialization() protectedReturns the JavaScript that initializes a plugin with its normalized constructor options.BootstrapWidgetTrait
registerClientEvents() protectedRegisters the JavaScript event handlers listed in $clientEvents at View::POS_END.BootstrapWidgetTrait
registerPlugin() protectedRegisters the plugin asset, the plugin initialization when needed, and the $clientEvents.BootstrapWidgetTrait
renderItem() protectedRenders a nav item.Nav
renderMenu() protectedRenders the menu toggled by an item.Nav
EventTypeDescriptionDefined by
EVENT_INITyii\base\EventAn event that is triggered when the widget is initialized via init(). Event name: 'init'. Available since 2.0.11.yii\base\Widget
EVENT_BEFORE_RUNyii\base\WidgetEventAn event raised right before executing a widget. Event name: 'beforeRun'. Available since 2.0.11.yii\base\Widget
EVENT_AFTER_RUNyii\base\WidgetEventAn event raised right after executing a widget. Event name: 'afterRun'. Available since 2.0.11.yii\base\Widget
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 by Url::to(); null renders no href. Defaults to #.
  • active: bool, forces the active state; otherwise it is detected from url.
  • disabled: bool, renders the link without href and with aria-disabled="true".
  • visible: bool, whether to render the item. Defaults to true.
  • encode: bool, whether to HTML-encode the label.
  • linkOptions: array, HTML attributes of the .nav-link element.
  • options: array, HTML attributes of the .nav-item element.
  • items: array|string, Menu::$items of the menu toggled by the item, or the menu HTML.
  • menuOptions: array, HTML attributes of the .menu panel.
public array<array-key, mixed>|null $params = null

Query 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.

public function init(): void

Initializes the widget, validates $layout and $style, and resolves $route and $params.

Throws

public function run(): string

Returns

string

Throws

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

ParameterTypeDescription
$styleyii2\extensions\bootstrap6\enum\NavStyle|string|null

Returns

list<string>

Throws

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

ParameterTypeDescription
$itemsarray<array-key, yii2\extensions\bootstrap6\item\MenuItem|string>Menu items, see Menu::$items.
$activeboolWhether the parent item is active.

Returns

array<array-key, yii2\extensions\bootstrap6\item\MenuItem|string>: Copies of the items, marked through MenuItem::withActive().

protected function isItemActive(yii2\extensions\bootstrap6\item\MenuItem|yii2\extensions\bootstrap6\item\NavItem $item): bool

Returns 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

ParameterTypeDescription
$itemyii2\extensions\bootstrap6\item\MenuItem|yii2\extensions\bootstrap6\item\NavItemNav item, or item of a menu toggled by the nav.

Returns

bool

protected function renderItem(yii2\extensions\bootstrap6\item\NavItem|string $item): string

Renders a nav item.

Parameters

ParameterTypeDescription
$itemyii2\extensions\bootstrap6\item\NavItem|stringNav item, or a string rendered as is.

Returns

string

protected function renderMenu(array<array-key, yii2\extensions\bootstrap6\item\MenuItem|string> $items, array<array-key, mixed> $options): string

Renders the menu toggled by an item.

Parameters

ParameterTypeDescription
$itemsarray<array-key, yii2\extensions\bootstrap6\item\MenuItem|string>Menu items, see Menu::$items.
$optionsarray<array-key, mixed>HTML attributes of the .menu panel.

Returns

string