PHPackages                             spatie/menu - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. spatie/menu

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

spatie/menu
===========

Html menu generator

3.2.1(11mo ago)7592.9M↑15.1%1026MITPHPPHP ^8.0CI passing

Since Feb 25Pushed 8mo ago22 watchersCompare

[ Source](https://github.com/spatie/menu)[ Packagist](https://packagist.org/packages/spatie/menu)[ Docs](https://github.com/spatie/menu)[ GitHub Sponsors](https://github.com/sponsors/spatie)[ Fund](https://spatie.be/open-source/support-us)[ RSS](/packages/spatie-menu/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (5)Versions (58)Used By (6)

 [   ![Logo for menu](https://camo.githubusercontent.com/1fefb940db0becc1d373d44863c6b16787953a03c24ca1d4e81ca02980830664/68747470733a2f2f7370617469652e62652f7061636b616765732f6865616465722f6d656e752f68746d6c2f6c696768742e776562703f31)  ](https://spatie.be/open-source?utm_source=github&utm_medium=banner&utm_campaign=menu)Fluent interface to build HTML menus in PHP
===========================================

[](#fluent-interface-to-build-html-menus-in-php)

[![Latest Version on Packagist](https://camo.githubusercontent.com/4161197b33e5b8470a315ab567108bc090fdfb50c7a39dfc8903a3b739a52cf2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7370617469652f6d656e752e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/menu)[![Tests](https://github.com/spatie/menu/workflows/Tests/badge.svg)](https://github.com/spatie/menu/actions?query=workflow%3ATests+branch%3Amaster)[![Total Downloads](https://camo.githubusercontent.com/8f653714a3972230de7354dd7ce771a4b6b91686dba1ddfa15d7ae5defdeee0f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370617469652f6d656e752e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/menu)

The `spatie/menu` package provides a fluent interface to build menus of any size in your php application. If you're building your app with Laravel, the [`spatie/laravel-menu`](https://github.com/spatie/laravel-menu) provides some extra treats.

If you're looking for a more flexible and renderless solution, maybe our spiritual successor [Laravel Navigation](https://github.com/spatie/laravel-navigation) is what you're looking for.

Documentation is available at .

Upgrading from version 1? There's a [guide](https://github.com/spatie/menu#upgrading-to-20) for that!

Support us
----------

[](#support-us)

[![](https://camo.githubusercontent.com/340a9bb7d2af6f05ee802088f3a051eec4a2b5ee4c52af55aafb508dba4bd549/68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f6d656e752e6a70673f743d31)](https://spatie.be/github-ad-click/menu)

We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).

Human Readable, Fluent Interface
--------------------------------

[](#human-readable-fluent-interface)

All classes provide a human readable, fluent interface (no array configuration). Additionally, you can opt for a more verbose and flexible syntax, or for convenience methods that cover most use cases.

```
Menu::new()
    ->add(Link::to('/', 'Home'))
    ->add(Link::to('/about', 'About'))
    ->add(Link::to('/contact', 'Contact'))
    ->add(Html::empty())
    ->render();

// Or just...
Menu::new()
    ->link('/', 'Home')
    ->link('/about', 'About')
    ->link('/contact', 'Contact')
    ->empty()
```

```

    Home
    About
    Contact

```

Or a More Programmatic Approach
-------------------------------

[](#or-a-more-programmatic-approach)

Menus can also be created through a reduce-like callable.

```
$pages = [
    '/' => 'Home',
    '/about' => 'About',
    '/contact' => 'Contact',
];

Menu::build($pages, function ($menu, $label, $url) {
    $menu->add($url, $label);
})->render();
```

```

    Home
    About
    Contact

```

Strong Control Over the Html Output
-----------------------------------

[](#strong-control-over-the-html-output)

You can programatically add html classes and attributes to any item in the menu, or to the menu itself.

```
Menu::new()
    ->addClass('navigation')
    ->add(Link::to('/', 'Home')->addClass('home-link'))
    ->add(Link::to('/about', 'About'))
    ->add(Link::to('/contact', 'Contact')->addParentClass('float-right'))
    ->wrap('div.wrapper')
```

```

        Home
        About
        Contact

id('navigation')
    ->add(Link::to('/', 'Home')->id('home-link'))
    ->add(Link::to('/about', 'About'))
    ->add(Link::to('/contact', 'Contact'))
```

```

    Home
    About
    Contact

```

Not Afraid of Depths
--------------------

[](#not-afraid-of-depths)

The menu supports submenus, which in turn can be nested infinitely.

```
Menu::new()
    ->link('/', 'Home')
    ->submenu('More', Menu::new()
        ->addClass('submenu')
        ->link('/about', 'About')
        ->link('/contact', 'Contact')
    );
```

```

    Home

        More

            About
            Contact

```

Some Extra Treats for Laravel Apps
----------------------------------

[](#some-extra-treats-for-laravel-apps)

The Laravel version of the menu package adds some extras like convenience methods for generating URLs and macros.

```
Menu::macro('main', function () {
    return Menu::new()
        ->action('HomeController@index', 'Home')
        ->action('AboutController@index', 'About')
        ->action('ContactController@index', 'Contact')
        ->setActiveFromRequest();
});
```

```

    {{ Menu::main() }}

```

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource).

Install
-------

[](#install)

You can install the package via composer:

```
composer require spatie/menu
```

Usage
-----

[](#usage)

Documentation is available at .

Upgrading to 2.0
----------------

[](#upgrading-to-20)

Upgrading to 2.0 should be pretty painless for most use cases.

### If you're just building menus...

[](#if-youre-just-building-menus)

- The `void` and `voidIf` have been removed. These can be replaced by `html` and `htmlIf`, with empty strings as their first arguments
- The `prefixLinks` and `prefixUrls` methods have been removed because they were too unpredictable in some case. There currently isn't an alternative for these, besides writing your own logic and applying it with `applyToAll`.

### If you're using custom `Item` implementations...

[](#if-youre-using-custom-item-implementations)

- The `HtmlAttributes` and `ParentAttributes` traits have been renamed to `HasHtmlAttributes` and `HasParentAttributes`.
- The `HasUrl` interface and trait has been removed. Url-related methods now also are part of the `Activatable` interface and trait.

### New features...

[](#new-features)

- Added the static `Menu::build` and non-static `Menu::fill` methods to create menu's from arrays.
- The `setActive` method on `Activatable` now also accepts a non-strict boolean or callable parameter to set `$active` to true or false.
- `Menu::html` and `Menu::htmlIf` now accept a `$parentAttributes` array as their second arguments.

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Testing
-------

[](#testing)

```
phpunit
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you've found a bug regarding security please mail  instead of using the issue tracker.

Postcardware
------------

[](#postcardware)

You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.

We publish all received postcards [on our company website](https://spatie.be/en/opensource/postcards).

Credits
-------

[](#credits)

- [Sebastian De Deyne](https://github.com/sebastiandedeyne)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

64

—

FairBetter than 99% of packages

Maintenance57

Moderate activity, may be stable

Popularity65

Solid adoption and visibility

Community39

Small or concentrated contributor base

Maturity82

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 57.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~60 days

Recently: every ~327 days

Total

57

Last Release

340d ago

Major Versions

0.6.6 → 1.0.02016-03-24

1.4.0 → 2.0.0-beta.12016-09-21

v1.x-dev → 2.9.02020-11-06

v2.x-dev → 3.0.02021-03-24

PHP version history (3 changes)0.1PHP ^7.0

2.9.0PHP ^7.1|^8.0

3.0.0PHP ^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/7535935?v=4)[Spatie](/maintainers/spatie)[@spatie](https://github.com/spatie)

---

Top Contributors

[![sebastiandedeyne](https://avatars.githubusercontent.com/u/1561079?v=4)](https://github.com/sebastiandedeyne "sebastiandedeyne (196 commits)")[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (68 commits)")[![rubenvanassche](https://avatars.githubusercontent.com/u/619804?v=4)](https://github.com/rubenvanassche "rubenvanassche (14 commits)")[![AdrianMrn](https://avatars.githubusercontent.com/u/12762044?v=4)](https://github.com/AdrianMrn "AdrianMrn (9 commits)")[![ccsliinc](https://avatars.githubusercontent.com/u/5481458?v=4)](https://github.com/ccsliinc "ccsliinc (8 commits)")[![rojtjo](https://avatars.githubusercontent.com/u/1123887?v=4)](https://github.com/rojtjo "rojtjo (5 commits)")[![AyoobMH](https://avatars.githubusercontent.com/u/37803924?v=4)](https://github.com/AyoobMH "AyoobMH (4 commits)")[![patinthehat](https://avatars.githubusercontent.com/u/5508707?v=4)](https://github.com/patinthehat "patinthehat (3 commits)")[![markwalet](https://avatars.githubusercontent.com/u/11446771?v=4)](https://github.com/markwalet "markwalet (3 commits)")[![jimirobaer](https://avatars.githubusercontent.com/u/8984769?v=4)](https://github.com/jimirobaer "jimirobaer (3 commits)")[![mrk-j](https://avatars.githubusercontent.com/u/1250622?v=4)](https://github.com/mrk-j "mrk-j (3 commits)")[![ralphjsmit](https://avatars.githubusercontent.com/u/59207045?v=4)](https://github.com/ralphjsmit "ralphjsmit (3 commits)")[![buddhaCode](https://avatars.githubusercontent.com/u/11678100?v=4)](https://github.com/buddhaCode "buddhaCode (2 commits)")[![alexbowers](https://avatars.githubusercontent.com/u/842974?v=4)](https://github.com/alexbowers "alexbowers (2 commits)")[![AlexVanderbist](https://avatars.githubusercontent.com/u/6287961?v=4)](https://github.com/AlexVanderbist "AlexVanderbist (2 commits)")[![Ayoub-Mabrouk](https://avatars.githubusercontent.com/u/77799760?v=4)](https://github.com/Ayoub-Mabrouk "Ayoub-Mabrouk (2 commits)")[![akoepcke](https://avatars.githubusercontent.com/u/5311185?v=4)](https://github.com/akoepcke "akoepcke (2 commits)")[![milwad-dev](https://avatars.githubusercontent.com/u/98118400?v=4)](https://github.com/milwad-dev "milwad-dev (2 commits)")[![swapnilsarwe](https://avatars.githubusercontent.com/u/166912?v=4)](https://github.com/swapnilsarwe "swapnilsarwe (1 commits)")[![BackEndTea](https://avatars.githubusercontent.com/u/14289961?v=4)](https://github.com/BackEndTea "BackEndTea (1 commits)")

---

Tags

htmlmenusphpspatiemenunavigation

###  Code Quality

TestsPest

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

![Health badge](/badges/spatie-menu/health.svg)

```
[![Health](https://phpackages.com/badges/spatie-menu/health.svg)](https://phpackages.com/packages/spatie-menu)
```

###  Alternatives

[verbb/navigation

Create navigation menus for your site.

90683.7k17](/packages/verbb-navigation)[kartik-v/yii2-widget-sidenav

An enhanced side navigation menu styled for bootstrap (sub repo split from yii2-widgets)

364.0M8](/packages/kartik-v-yii2-widget-sidenav)[caffeinated/menus

Laravel Menus

134159.5k5](/packages/caffeinated-menus)[akaunting/laravel-menu

Menu and sidebar management package for Laravel

38233.8k](/packages/akaunting-laravel-menu)[belugadigital/kirby-navigation

Kirby 5 field for hierarchical menus with drag &amp; drop level indentation.

8713.4k](/packages/belugadigital-kirby-navigation)[kodicomponents/navigation

The KodiCMS Support package.

12232.6k10](/packages/kodicomponents-navigation)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
