Menu | menu | Spatie

 SPATIE

  HTML Menu Generator
======================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Menu](https://spatie.be/docs/menu/v1)  Api  Menu

 Version   v3   v2   v1

 Other versions for crawler [v3](https://spatie.be/docs/menu/v3) [v2](https://spatie.be/docs/menu/v2) [v1](https://spatie.be/docs/menu/v1)

  Menu
- [ Introduction ](https://spatie.be/docs/menu/v1/introduction)
- [ Postcardware ](https://spatie.be/docs/menu/v1/postcardware)
- [ Requirements ](https://spatie.be/docs/menu/v1/requirements)
- [ Installation and Setup ](https://spatie.be/docs/menu/v1/installation-and-setup)
- [ Questions &amp; Issues ](https://spatie.be/docs/menu/v1/questions-and-issues)
- [ Changelog ](https://spatie.be/docs/menu/v1/changelog)
- [ About Us ](https://spatie.be/docs/menu/v1/about-us)

Basic usage
-----------

- [ Your First Menu ](https://spatie.be/docs/menu/v1/basic-usage/your-first-menu)
- [ Adding Items ](https://spatie.be/docs/menu/v1/basic-usage/adding-items)

Items in depth
--------------

- [ Active Items ](https://spatie.be/docs/menu/v1/items-in-depth/active-items)
- [ Manipulating Items ](https://spatie.be/docs/menu/v1/items-in-depth/manipulating-items)
- [ Conditionally Adding Items ](https://spatie.be/docs/menu/v1/items-in-depth/conditionally-adding-items)

Controlling the html output
---------------------------

- [ Item Attributes ](https://spatie.be/docs/menu/v1/controlling-the-html-output/item-attributes)
- [ Parent Attributes ](https://spatie.be/docs/menu/v1/controlling-the-html-output/parent-attributes)
- [ Appending and Prepending Html ](https://spatie.be/docs/menu/v1/controlling-the-html-output/appending-and-prepending-html)

Menus in your Laravel app
-------------------------

- [ Convenience Methods ](https://spatie.be/docs/menu/v1/menus-in-your-laravel-app/convenience-methods)
- [ Conditional Items Based on Permissions ](https://spatie.be/docs/menu/v1/menus-in-your-laravel-app/conditional-items-based-on-permissions)
- [ Using Macros ](https://spatie.be/docs/menu/v1/menus-in-your-laravel-app/using-macros)

Examples
--------

- [ Bootstrap Menu ](https://spatie.be/docs/menu/v1/examples/bootstrap-menu)

API
---

- [ Menu ](https://spatie.be/docs/menu/v1/api/menu)
- [ Link ](https://spatie.be/docs/menu/v1/api/link)
- [ Html ](https://spatie.be/docs/menu/v1/api/html)

      You are viewing the documentation for **an older version** of this package. You can check the version you are using with the following command:

 `                                    composer show spatie/menu                                                                                                                                                                                                                                    `

Menu
====

`Spatie\Menu\Menu`
------------------------------------------------------------------------------------------------------

- Implements `Countable`, `Spatie\Menu\HasHtmlAttributes`, `Spatie\Menu\HasParentAttributes`, `Spatie\Menu\Item`

### new

```
/**
 * Create a new menu, optionally prefilled with items.
 *
 * @param array $items
 *
 * @return static
 */
public static function new(array $items = [])
```

### `add`

```
/**
 * Add an item to the menu. This also applies all registered filters to the
 * item.
 *
 * @param \Spatie\Menu\Item $item
 *
 * @return $this
 */
public function add(Item $item)
```

### `addIf`

```
/**
 * Add an item to the menu if a (non-strict) condition is met.
 *
 * @param bool $condition
 * @param \Spatie\Menu\Item $item
 *
 * @return $this
 */
public function addIf($condition, Item $item)
```

### `link`

```
/**
 * Shortcut function to add a plain link to the menu.
 *
 * @param string $url
 * @param string $text
 *
 * @return $this
 */
public function link(string $url, string $text)
```

### `linkIf`

```
/**
 * Add a link to the menu if a (non-strict) condition is met.
 *
 * @param bool $condition
 * @param string $url
 * @param string $text
 *
 * @return $this
 */
public function linkIf($condition, string $url, string $text)
```

### `html`

```
/**
 * Shortcut function to add raw html to the menu.
 *
 * @param string $html
 *
 * @return $this
 */
public function html(string $html)
```

### `htmlIf`

```
/**
 * Add a chunk of html if a (non-strict) condition is met.
 *
 * @param bool $condition
 * @param string $html
 *
 * @return $this
 */
public function htmlIf($condition, string $html)
```

### `each`

```
/**
 * Iterate over all the items and apply a callback. If you typehint the
 * item parameter in the callable, it wil only be applied to items of that
 * type.
 *
 * @param callable $callable
 *
 * @return $this
 */
public function each(callable $callable)
```

### `registerFilter`

```
/**
 * Register a filter to the menu. When an item is added, all filters will be
 * applied to the item. If you typehint the item parameter in the callable, it
 * will only be applied to items of that type.
 *
 * @param callable $callable
 *
 * @return $this
 */
public function registerFilter(callable $callable)
```

### `applyToAll`

```
/**
 * Apply a callable to all existing items, and register it as a filter so it
 * will get applied to all new items too. If you typehint the item parameter
 * in the callable, it wil only be applied to items of that type.
 *
 * @param callable $callable
 *
 * @return $this
 */
public function applyToAll(callable $callable)
```

### `prefixLinks`

```
/**
 * Prefix all the links in the menu.
 *
 * @param string $prefix
 *
 * @return $this
 */
public function prefixLinks(string $prefix)
```

### `prepend`

```
/**
 * Prepend the menu with a string of html on render.
 *
 * @param string $prepend
 *
 * @return $this
 */
public function prepend(string $prepend)
```

### `prependIf`

```
/**
 * Prepend the menu with a string of html on render if a certain condition is
 * met.
 *
 * @param bool $condition
 * @param string $prepend
 *
 * @return $this
 */
public function prependIf(bool $condition, string $prepend)
```

### `append`

```
/**
 * Append a string of html to the menu on render.
 *
 * @param string $append
 *
 * @return $this
 */
public function append(string $append)
```

### `appendIf`

```
/**
 * Append the menu with a string of html on render if a certain condition is
 * met.
 *
 * @param bool $condition
 * @param string $append
 *
 * @return static
 */
public function appendIf(bool $condition, string $append)
```

### `isActive`

```
/**
 * Determine whether the menu is active.
 *
 * @return bool
 */
public function isActive() : bool
```

### `setActive`

```
/**
 * Set multiple items in the menu as active based on a callable that filters
 * through items. If you typehint the item parameter in the callable, it will
 * only be applied to items of that type.
 *
 * @param callable|string $urlOrCallable
 * @param string $root
 *
 * @return $this
 */
public function setActive($urlOrCallable, string $root = '/')
```

### `setActiveFromUrl`

```
/**
 * Set all relevant children active based on the current request's URL.
 *
 * /, /about, /contact => request to /about will set the about link active.
 *
 * /en, /en/about, /en/contact => request to /en won't set /en active if the
 *                                request root is set to /en.
 *
 * @param string $url The current request url.
 * @param string $root If the link's URL is an exact match with the request
 *                     root, the link won't be set active. This behavior is
 *                     to avoid having home links active on every request.
 *
 * @return $this
 */
public function setActiveFromUrl(string $url, string $root = '/')
```

### `setActiveFromCallable`

```
/**
 * @param callable $callable
 *
 * @return $this
 */
public function setActiveFromCallable(callable $callable)
```

### `setActiveClass`

```
/**
 * Set the class name that will be used on active items for this menu.
 *
 * @param string $class
 *
 * @return $this
 */
public function setActiveClass(string $class)
```

### `render`

```
/**
 * Render the menu.
 *
 * @return string
 */
public function render() : string
```

### `count`

```
/**
 * The amount of items in the menu.
 *
 * @return int
 */
public function count() : int
```

### `setAttribute`

```
/**
 * @param string $attribute
 * @param string $value
 *
 * @return $this
 */
public function setAttribute(string $attribute, string $value = '')
```

### `addClass`

```
/**
 * @param string $class
 *
 * @return $this
 */
public function addClass(string $class)
```

### `getParentAttributes`

```
/**
 * Return an array of attributes to apply on the parent. This generally means
 * the attributes that should be applied on the  tag.
 *
 * @return array
 */
public function getParentAttributes() : array
```

### `setParentAttribute`

```
/**
 * @param string $attribute
 * @param string $value
 *
 * @return $this
 */
public function setParentAttribute(string $attribute, string $value = '')
```

### `addParentClass`

```
/**
 * @param string $class
 *
 * @return $this
 */
public function addParentClass(string $class)
```

### `addItemClass`

```
/**
 * Add a class to all items in the menu.
 *
 * @param string $class
 *
 * @return $this
 */
public function addItemClass(string $class)
```

### `setItemAttribute`

```
/**
 * Set an attribute on all items in the menu.
 *
 * @param string $attribute
 * @param string $value
 *
 * @return $this
 */
public function setItemAttribute(string $attribute, string $value = '')
```

### `addItemParentClass`

```
/**
 * Add a parent class to all items in the menu.
 *
 * @param string $class
 *
 * @return $this
 */
public function addItemParentClass(string $class)
```

### `setItemParentAttribute`

```
/**
 * Add a parent attribute to all items in the menu.
 *
 * @param string $attribute
 * @param string $value
 *
 * @return $this
 */
public function setItemParentAttribute(string $attribute, string $value = '')
```

### `blueprint`

```
/**
 * Create a empty blueprint of the menu (copies `filters` and `activeClass`).
 *
 * @return static
 */
public function blueprint()
```

`Spatie\Menu\Laravel\Menu`
----------------------------------------------------------------------------------------------------------------------------

- Extends `Spatie\Menu\Laravel\Menu`
- Uses `Illuminate\Support\Traits\Macroable`

### `setActiveFromRequest`

```
/**
 * Set all relevant children active based on the current request's URL.
 *
 * /, /about, /contact => request to /about will set the about link active.
 *
 * /en, /en/about, /en/contact => request to /en won't set /en active if the
 *                                request root is set to /en.
 *
 * @param string $requestRoot If the link's URL is an exact match with the
 *                            request root, the link won't be set active.
 *                            This behavior is to avoid having home links
 *                            active on every request.
 *
 * @return $this
 */
public function setActiveFromRequest(string $requestRoot = '')
```

### `addIfCan`

```
/**
 * @param string|array $authorization
 * @param \Spatie\Menu\Item $item
 *
 * @return $this
 */
public function addIfCan($authorization, Item $item)
```

### `linkIfCan`

```
/**
 * @param string|array $authorization
 * @param string $url
 * @param string $text
 *
 * @return $this
 */
public function linkIfCan($authorization, string $url, string $text)
```

### `htmlIfCan`

```
/**
 * @param string|array $authorization
 * @param string $html
 *
 * @return \Spatie\Menu\Laravel\Menu
 */
public function htmlIfCan($authorization, string $html)
```

### `url`

```
/**
 * @param string $path
 * @param string $text
 * @param array $parameters
 * @param bool|null $secure
 *
 * @return $this
 */
public function url(string $path, string $text, array $parameters = [], $secure = null)
```

### `urlIf`

```
/**
 * @param bool $condition
 * @param string $path
 * @param string $text
 * @param array $parameters
 * @param bool|null $secure
 *
 * @return $this
 */
public function urlIf($condition, string $path, string $text, array $parameters = [], $secure = null)
```

### `urlIfCan`

```
/**
 * @param string|array $authorization
 * @param string $path
 * @param string $text
 * @param array $parameters
 * @param bool|null $secure
 *
 * @return $this
 */
public function urlIfCan(
    $authorization,
    string $path,
    string $text,
    array $parameters = [],
    $secure = null
)
```

### `action`

```
/**
 * @param string $action
 * @param string $text
 * @param array $parameters
 * @param bool $absolute
 *
 * @return $this
 */
public function action(string $action, string $text, array $parameters = [], bool $absolute = true)
```

### `actionIf`

```
/**
 * @param bool $condition
 * @param string $action
 * @param string $text
 * @param array $parameters
 * @param bool $absolute
 *
 * @return $this
 */
public function actionIf($condition, string $action, string $text, array $parameters = [], bool $absolute = true)
```

### `actionIfCan`

```
/**
 * @param string|array $authorization
 * @param string $action
 * @param string $text
 * @param array $parameters
 * @param bool $absolute
 *
 * @return $this
 */
public function actionIfCan(
    $authorization,
    string $action,
    string $text,
    array $parameters = [],
    bool $absolute = true
)
```

### `route`

```
/**
 * @param string $name
 * @param string $text
 * @param array $parameters
 * @param bool $absolute
 * @param \Illuminate\Routing\Route|null $route
 *
 * @return $this
 */
public function route(string $name, string $text, array $parameters = [], bool $absolute = true, $route = null)
```

### `routeIf`

```
/**
 * @param bool $condition
 * @param string $name
 * @param string $text
 * @param array $parameters
 * @param bool $absolute
 * @param \Illuminate\Routing\Route|null $route
 *
 * @return $this
 */
public function routeIf($condition, string $name, string $text, array $parameters = [], bool $absolute = true, $route = null)
```

### `routeIfCan`

```
/**
 * @param string|array $authorization
 * @param string $name
 * @param string $text
 * @param array $parameters
 * @param bool $absolute
 * @param \Illuminate\Routing\Route|null $route
 *
 * @return $this
 */
public function routeIfCan(
    $authorization,
    string $name,
    string $text,
    array $parameters = [],
    bool $absolute = true,
    $route = null
)
```

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
