Skip to content
PHP Ecosystem
Explore

NavItem

API reference › Bootstrap 6 › item › NavItem

final readonly classyii2\extensions\bootstrap6\item\NavItem
Uses traitsItemTrait
Sourcesrc/item/NavItem.php

Represents a Nav item: a link, or a toggle that opens a Menu.

PropertyTypeDescriptionDefined by
$active readonly constructorbool|nullWhether the item is active, or null to detect it from the $url route. Default: null.NavItem
$disabled readonly constructorboolWhether the item is disabled; it takes precedence over $active. Default: false.NavItem
$encode readonly constructorbool|nullWhether to HTML-encode $label, or null to use Nav::$encodeLabels. Default: null.NavItem
$items readonly constructorarray<array-key, MenuItem|string>|stringItems of the menu toggled by the item, or the menu HTML; [] renders a plain link. Default: [].NavItem
$label readonly constructorstring|StringableLabel of the .nav-link element.NavItem
$linkOptions readonly constructorarray<array-key, mixed>HTML attributes of the .nav-link element. Default: [].NavItem
$menuOptions readonly constructorarray<array-key, mixed>HTML attributes of the .menu panel. Default: [].NavItem
$options readonly constructorarray<array-key, mixed>HTML attributes of the .nav-item element. Default: [].NavItem
$url readonly constructorarray<array-key, mixed>|string|nullURL processed by Url::to(), or null for no href; a route array also drives the active state detection. Default: '#'.NavItem
$visible readonly constructorboolWhether to render the item. Default: true.NavItem
MethodDescriptionDefined by
fromArray() staticCreates an item from the array configuration documented in Nav::$items.NavItem
menu() staticCreates a toggle that opens a Menu.NavItem
to() staticCreates a link item.NavItem
withActive()Returns a copy with the given active state.NavItem
withDisabled()Returns a copy with the given disabled state.NavItem
withEncode()Returns a copy with the given encoding flag.NavItem
withItems()Returns a copy with the given menu items or menu HTML.NavItem
withLabel()Returns a copy with the given label.NavItem
withLinkOptions()Returns a copy with the given .nav-link attributes.NavItem
withMenuOptions()Returns a copy with the given .menu panel attributes.NavItem
withOptions()Returns a copy with the given .nav-item attributes.NavItem
withUrl()Returns a copy with the given URL.NavItem
withVisible()Returns a copy with the given visibility.NavItem
public static function fromArray(array<array-key, mixed> $config): self

Creates an item from the array configuration documented in Nav::$items.

A missing url defaults to #; an explicit null renders no href. Nested items arrays are converted through MenuItem::fromArray().

Parameters

ParameterTypeDescription
$configarray<array-key, mixed>Item configuration.

Returns

self

Throws

public static function menu(string|Stringable $label, array<array-key, array<string, mixed>|yii2\extensions\bootstrap6\item\MenuItem|string>|string $items): self

Creates a toggle that opens a Menu.

Parameters

ParameterTypeDescription
$labelstring|StringableToggle label.
$itemsarray<array-key, array<string, mixed>|MenuItem|string>|stringMenu items, or the menu HTML; arrays are converted through MenuItem::fromArray().

Returns

self

public static function to(string|Stringable $label, array<array-key, mixed>|string|null $url = '#'): self

Creates a link item.

Parameters

ParameterTypeDescription
$labelstring|StringableLink label.
$urlarray<array-key, mixed>|string|nullURL processed by Url::to(); null renders no href.

Returns

self

public function withActive(bool|null $active): self

Returns a copy with the given active state.

Parameters

ParameterTypeDescription
$activebool|null

Returns

self

public function withDisabled(bool $disabled): self

Returns a copy with the given disabled state.

Parameters

ParameterTypeDescription
$disabledbool

Returns

self

public function withEncode(bool|null $encode): self

Returns a copy with the given encoding flag.

Parameters

ParameterTypeDescription
$encodebool|null

Returns

self

public function withItems(array<array-key, array<string, mixed>|yii2\extensions\bootstrap6\item\MenuItem|string>|string $items): self

Returns a copy with the given menu items or menu HTML.

Parameters

ParameterTypeDescription
$itemsarray<array-key, array<string, mixed>|MenuItem|string>|stringMenu items, or the menu HTML; arrays are converted through MenuItem::fromArray().

Returns

self

public function withLabel(string|Stringable $label): self

Returns a copy with the given label.

Parameters

ParameterTypeDescription
$labelstring|Stringable

Returns

self

public function withLinkOptions(array<array-key, mixed> $linkOptions): self

Returns a copy with the given .nav-link attributes.

Parameters

ParameterTypeDescription
$linkOptionsarray<array-key, mixed>HTML attributes of the .nav-link element.

Returns

self

public function withMenuOptions(array<array-key, mixed> $menuOptions): self

Returns a copy with the given .menu panel attributes.

Parameters

ParameterTypeDescription
$menuOptionsarray<array-key, mixed>HTML attributes of the .menu panel.

Returns

self

public function withOptions(array<array-key, mixed> $options): self

Returns a copy with the given .nav-item attributes.

Parameters

ParameterTypeDescription
$optionsarray<array-key, mixed>HTML attributes of the .nav-item element.

Returns

self

public function withUrl(array<array-key, mixed>|string|null $url): self

Returns a copy with the given URL.

Parameters

ParameterTypeDescription
$urlarray<array-key, mixed>|string|nullURL processed by Url::to().

Returns

self

public function withVisible(bool $visible): self

Returns a copy with the given visibility.

Parameters

ParameterTypeDescription
$visiblebool

Returns

self