NavItem
API reference › Bootstrap 6 › item › NavItem
Represents a Nav item: a link, or a toggle that opens a Menu.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$active readonly constructor | bool| | Whether the item is active, or null to detect it from the $url route. Default: null. | NavItem |
$disabled readonly constructor | bool | Whether the item is disabled; it takes precedence over $active. Default: false. | NavItem |
$encode readonly constructor | bool| | Whether to HTML-encode $label, or null to use Nav::$encodeLabels. Default: null. | NavItem |
$items readonly constructor | array<array-key, MenuItem| | Items of the menu toggled by the item, or the menu HTML; [] renders a plain link. Default: []. | NavItem |
$label readonly constructor | string|Stringable | Label of the .nav-link element. | NavItem |
$linkOptions readonly constructor | array<array-key, | HTML attributes of the .nav-link element. Default: []. | NavItem |
$menuOptions readonly constructor | array<array-key, | HTML attributes of the .menu panel. Default: []. | NavItem |
$options readonly constructor | array<array-key, | HTML attributes of the .nav-item element. Default: []. | NavItem |
$url readonly constructor | array<array-key, | URL processed by Url::to(), or null for no href; a route array also drives the active state detection. Default: '#'. | NavItem |
$visible readonly constructor | bool | Whether to render the item. Default: true. | NavItem |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
fromArray() static | Creates an item from the array configuration documented in Nav::$items. | NavItem |
menu() static | Creates a toggle that opens a Menu. | NavItem |
to() static | Creates 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 |
Method details
Section titled “Method details”fromArray()
Section titled “fromArray() ”public static function fromArray(array<array-key, mixed> $config): selfCreates 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
| Parameter | Type | Description |
|---|---|---|
$config | array<array-key, | Item configuration. |
Returns
self
Throws
yii\base\InvalidConfigException: if the item, or one of its menu items, has nostring|Stringablelabel.
menu()
Section titled “menu() ”public static function menu(string|Stringable $label, array<array-key, array<string, mixed>|yii2\extensions\bootstrap6\item\MenuItem|string>|string $items): selfCreates a toggle that opens a Menu.
Parameters
| Parameter | Type | Description |
|---|---|---|
$label | string|Stringable | Toggle label. |
$items | array<array-key,MenuItem| | Menu 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 = '#'): selfCreates a link item.
Parameters
| Parameter | Type | Description |
|---|---|---|
$label | string|Stringable | Link label. |
$url | array<array-key, | URL processed by Url::to(); null renders no href. |
Returns
self
withActive()
Section titled “withActive() ”public function withActive(bool|null $active): selfReturns a copy with the given active state.
Parameters
| Parameter | Type | Description |
|---|---|---|
$active | bool| |
Returns
self
withDisabled()
Section titled “withDisabled() ”public function withDisabled(bool $disabled): selfReturns a copy with the given disabled state.
Parameters
| Parameter | Type | Description |
|---|---|---|
$disabled | bool |
Returns
self
withEncode()
Section titled “withEncode() ”public function withEncode(bool|null $encode): selfReturns a copy with the given encoding flag.
Parameters
| Parameter | Type | Description |
|---|---|---|
$encode | bool| |
Returns
self
withItems()
Section titled “withItems() ”public function withItems(array<array-key, array<string, mixed>|yii2\extensions\bootstrap6\item\MenuItem|string>|string $items): selfReturns a copy with the given menu items or menu HTML.
Parameters
| Parameter | Type | Description |
|---|---|---|
$items | array<array-key,MenuItem| | Menu items, or the menu HTML; arrays are converted through MenuItem::fromArray(). |
Returns
self
withLabel()
Section titled “withLabel() ”public function withLabel(string|Stringable $label): selfReturns a copy with the given label.
withLinkOptions()
Section titled “withLinkOptions() ”public function withLinkOptions(array<array-key, mixed> $linkOptions): selfReturns a copy with the given .nav-link attributes.
Parameters
| Parameter | Type | Description |
|---|---|---|
$linkOptions | array<array-key, | HTML attributes of the .nav-link element. |
Returns
self
withMenuOptions()
Section titled “withMenuOptions() ”public function withMenuOptions(array<array-key, mixed> $menuOptions): selfReturns a copy with the given .menu panel attributes.
Parameters
| Parameter | Type | Description |
|---|---|---|
$menuOptions | array<array-key, | HTML attributes of the .menu panel. |
Returns
self
withOptions()
Section titled “withOptions() ”public function withOptions(array<array-key, mixed> $options): selfReturns a copy with the given .nav-item attributes.
Parameters
| Parameter | Type | Description |
|---|---|---|
$options | array<array-key, | HTML attributes of the .nav-item element. |
Returns
self
withUrl()
Section titled “withUrl() ”public function withUrl(array<array-key, mixed>|string|null $url): selfReturns a copy with the given URL.
Parameters
| Parameter | Type | Description |
|---|---|---|
$url | array<array-key, | URL processed by Url::to(). |
Returns
self
withVisible()
Section titled “withVisible() ”public function withVisible(bool $visible): selfReturns a copy with the given visibility.
Parameters
| Parameter | Type | Description |
|---|---|---|
$visible | bool |
Returns
self