# MenuItem

> Represents a Menu item: a link, a button, a submenu, a header, a text, or a divider.

[API reference](/docs/api/index.md) › [Bootstrap 6](/docs/extensions/bootstrap6/reference/index.md) › [item](/docs/extensions/bootstrap6/reference/item/index.md) › MenuItem

| final readonly class | `yii2\extensions\bootstrap6\item\MenuItem` |
| --- | --- |
| Uses traits | [`ItemTrait`](/docs/extensions/bootstrap6/reference/item/itemtrait/index.md) |
| Source | [`src/item/MenuItem.php`](https://github.com/yii2-extensions/bootstrap6/blob/main/src/item/MenuItem.php#L18) |

Represents a `Menu` item: a link, a button, a submenu, a header, a text, or a divider.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-active"></span>`$active` readonly constructor | `bool\|null` | Whether the item is active, or `null` to let `Nav` detect it from [`$url`](/docs/extensions/bootstrap6/reference/item/menuitem/index.md#property-url). Default: `null`. | `MenuItem` |
| <span id="property-disabled"></span>`$disabled` readonly constructor | `bool` | Whether the item is disabled; it takes precedence over [`$active`](/docs/extensions/bootstrap6/reference/item/menuitem/index.md#property-active). Default: `false`. | `MenuItem` |
| <span id="property-encode"></span>`$encode` readonly constructor | `bool\|null` | Whether to HTML-encode [`$label`](/docs/extensions/bootstrap6/reference/item/menuitem/index.md#property-label), or `null` to use the widget `encodeLabels`. Default: `null`. | `MenuItem` |
| <span id="property-items"></span>`$items` readonly constructor | `array<array-key,` [`MenuItem`](/docs/extensions/bootstrap6/reference/item/menuitem/index.md)`\|string>` | Items of the nested submenu; the string `-` renders a divider, any other string is rendered as is. Default: `[]`. | `MenuItem` |
| <span id="property-label"></span>`$label` readonly constructor | `string\|`[`Stringable`](https://www.php.net/manual/en/class.stringable.php) | Label of a link, button, or submenu toggle, or content of a header or text; empty for a divider. | `MenuItem` |
| <span id="property-linkOptions"></span>`$linkOptions` readonly constructor | `array<array-key, mixed>` | HTML attributes of the `.menu-item` element. Default: `[]`. | `MenuItem` |
| <span id="property-options"></span>`$options` readonly constructor | `array<array-key, mixed>` | HTML attributes of the `.submenu` wrapper, header, text, or divider. Default: `[]`. | `MenuItem` |
| <span id="property-submenuOptions"></span>`$submenuOptions` readonly constructor | `array<array-key, mixed>` | HTML attributes of the nested `.menu`, merged over the widget `submenuOptions`. Default: `[]`. | `MenuItem` |
| <span id="property-type"></span>`$type` readonly constructor | [`yii2\extensions\bootstrap6\enum\MenuItemType`](/docs/extensions/bootstrap6/reference/enum/menuitemtype/index.md) | Kind of the item, which selects the rendered element. | `MenuItem` |
| <span id="property-url"></span>`$url` readonly constructor | `array<array-key, mixed>\|string\|null` | URL of a link, processed by [`Url::to()`](/docs/api/yii/helpers/baseurl/index.md#method-to). Default: `null`. | `MenuItem` |
| <span id="property-visible"></span>`$visible` readonly constructor | `bool` | Whether to render the item. Default: `true`. | `MenuItem` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`button()`](#method-button) static | Creates a `<button>` item. | `MenuItem` |
| [`divider()`](#method-divider) static | Creates an `<hr class="menu-divider">` item. | `MenuItem` |
| [`fromArray()`](#method-fromArray) static | Creates an item from the array configuration documented in `Menu::$items`. | `MenuItem` |
| [`header()`](#method-header) static | Creates an `<h6 class="menu-header">` item. | `MenuItem` |
| [`link()`](#method-link) static | Creates a link item. | `MenuItem` |
| [`submenu()`](#method-submenu) static | Creates a submenu: a toggle button followed by a nested `.menu`, inside a `.submenu` wrapper. | `MenuItem` |
| [`text()`](#method-text) static | Creates a `<span class="menu-text">` item. | `MenuItem` |
| [`withActive()`](#method-withActive) | Returns a copy with the given active state. | `MenuItem` |
| [`withDisabled()`](#method-withDisabled) | Returns a copy with the given disabled state. | `MenuItem` |
| [`withEncode()`](#method-withEncode) | Returns a copy with the given encoding flag. | `MenuItem` |
| [`withItems()`](#method-withItems) | Returns a copy with the given nested items. | `MenuItem` |
| [`withLabel()`](#method-withLabel) | Returns a copy with the given label or content. | `MenuItem` |
| [`withLinkOptions()`](#method-withLinkOptions) | Returns a copy with the given `.menu-item` attributes. | `MenuItem` |
| [`withOptions()`](#method-withOptions) | Returns a copy with the given wrapper, header, text, or divider attributes. | `MenuItem` |
| [`withSubmenuOptions()`](#method-withSubmenuOptions) | Returns a copy with the given nested `.menu` attributes. | `MenuItem` |
| [`withType()`](#method-withType) | Returns a copy with the given kind. | `MenuItem` |
| [`withUrl()`](#method-withUrl) | Returns a copy with the given URL. | `MenuItem` |
| [`withVisible()`](#method-withVisible) | Returns a copy with the given visibility. | `MenuItem` |

## Method details

### button() <span id="method-button"></span>

```php
public static function button(string|Stringable $label): self
```

Creates a `<button>` item.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$label` | `string\|`[`Stringable`](https://www.php.net/manual/en/class.stringable.php) | Button label. |

**Returns**

`self`

### divider() <span id="method-divider"></span>

```php
public static function divider(): self
```

Creates an `<hr class="menu-divider">` item.

**Returns**

`self`

### fromArray() <span id="method-fromArray"></span>

```php
public static function fromArray(array<array-key, mixed> $config): self
```

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

The `divider`, `header`, and `text` keys select those kinds, in that order; otherwise, a non-empty `items` key
selects a submenu, a `url` key (even `null`) a link, and its absence a button. Nested `items` arrays are
converted recursively; strings are kept.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$config` | `array<array-key, mixed>` | Item configuration. |

**Returns**

`self`

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if a link, button, or submenu has no `string|Stringable` `label`.

### header() <span id="method-header"></span>

```php
public static function header(string|Stringable $content): self
```

Creates an `<h6 class="menu-header">` item.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$content` | `string\|`[`Stringable`](https://www.php.net/manual/en/class.stringable.php) | Header content. |

**Returns**

`self`

### link() <span id="method-link"></span>

```php
public static function link(string|Stringable $label, array<array-key, mixed>|string|null $url): self
```

Creates a link item.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$label` | `string\|`[`Stringable`](https://www.php.net/manual/en/class.stringable.php) | Link label. |
| `$url` | `array<array-key, mixed>\|string\|null` | Link URL, processed by [`Url::to()`](/docs/api/yii/helpers/baseurl/index.md#method-to); `null` renders no `href`. |

**Returns**

`self`

### submenu() <span id="method-submenu"></span>

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

Creates a submenu: a toggle button followed by a nested `.menu`, inside a `.submenu` wrapper.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$label` | `string\|`[`Stringable`](https://www.php.net/manual/en/class.stringable.php) | Toggle label. |
| `$items` | `array<array-key, array<string, mixed>\|`[`MenuItem`](/docs/extensions/bootstrap6/reference/item/menuitem/index.md)`\|string>` | Items of the nested menu; arrays are converted through [`fromArray()`](/docs/extensions/bootstrap6/reference/item/menuitem/index.md#method-fromArray). |

**Returns**

`self`

### text() <span id="method-text"></span>

```php
public static function text(string|Stringable $content): self
```

Creates a `<span class="menu-text">` item.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$content` | `string\|`[`Stringable`](https://www.php.net/manual/en/class.stringable.php) | Text content. |

**Returns**

`self`

### withActive() <span id="method-withActive"></span>

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

Returns a copy with the given active state.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$active` | `bool\|null` |  |

**Returns**

`self`

### withDisabled() <span id="method-withDisabled"></span>

```php
public function withDisabled(bool $disabled): self
```

Returns a copy with the given disabled state.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$disabled` | `bool` |  |

**Returns**

`self`

### withEncode() <span id="method-withEncode"></span>

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

Returns a copy with the given encoding flag.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$encode` | `bool\|null` |  |

**Returns**

`self`

### withItems() <span id="method-withItems"></span>

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

Returns a copy with the given nested items.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$items` | `array<array-key, array<string, mixed>\|`[`MenuItem`](/docs/extensions/bootstrap6/reference/item/menuitem/index.md)`\|string>` | Items of the nested menu; arrays are converted through [`fromArray()`](/docs/extensions/bootstrap6/reference/item/menuitem/index.md#method-fromArray). |

**Returns**

`self`

### withLabel() <span id="method-withLabel"></span>

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

Returns a copy with the given label or content.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$label` | `string\|`[`Stringable`](https://www.php.net/manual/en/class.stringable.php) |  |

**Returns**

`self`

### withLinkOptions() <span id="method-withLinkOptions"></span>

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

Returns a copy with the given `.menu-item` attributes.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$linkOptions` | `array<array-key, mixed>` | HTML attributes of the `.menu-item` element. |

**Returns**

`self`

### withOptions() <span id="method-withOptions"></span>

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

Returns a copy with the given wrapper, header, text, or divider attributes.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$options` | `array<array-key, mixed>` | HTML attributes. |

**Returns**

`self`

### withSubmenuOptions() <span id="method-withSubmenuOptions"></span>

```php
public function withSubmenuOptions(array<array-key, mixed> $submenuOptions): self
```

Returns a copy with the given nested `.menu` attributes.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$submenuOptions` | `array<array-key, mixed>` | HTML attributes of the nested `.menu`. |

**Returns**

`self`

### withType() <span id="method-withType"></span>

```php
public function withType(yii2\extensions\bootstrap6\enum\MenuItemType $type): self
```

Returns a copy with the given kind.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$type` | [`yii2\extensions\bootstrap6\enum\MenuItemType`](/docs/extensions/bootstrap6/reference/enum/menuitemtype/index.md) |  |

**Returns**

`self`

### withUrl() <span id="method-withUrl"></span>

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

Returns a copy with the given URL.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$url` | `array<array-key, mixed>\|string\|null` | URL processed by [`Url::to()`](/docs/api/yii/helpers/baseurl/index.md#method-to). |

**Returns**

`self`

### withVisible() <span id="method-withVisible"></span>

```php
public function withVisible(bool $visible): self
```

Returns a copy with the given visibility.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$visible` | `bool` |  |

**Returns**

`self`