{"id":13566556,"url":"https://github.com/spatie/menu","last_synced_at":"2025-05-14T20:04:58.609Z","repository":{"id":43642905,"uuid":"51363080","full_name":"spatie/menu","owner":"spatie","description":"Html menu generator","archived":false,"fork":false,"pushed_at":"2024-06-17T06:46:16.000Z","size":334,"stargazers_count":748,"open_issues_count":0,"forks_count":99,"subscribers_count":22,"default_branch":"main","last_synced_at":"2025-04-14T02:58:11.319Z","etag":null,"topics":["html","menus","php"],"latest_commit_sha":null,"homepage":"https://freek.dev/414-a-modern-package-to-generate-html-menus","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/spatie.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"spatie","custom":"https://spatie.be/open-source/support-us"}},"created_at":"2016-02-09T11:34:31.000Z","updated_at":"2025-04-05T15:32:45.000Z","dependencies_parsed_at":"2024-06-17T07:55:24.758Z","dependency_job_id":"69ea376d-e6cc-46d1-a4da-2af3d542145c","html_url":"https://github.com/spatie/menu","commit_stats":{"total_commits":302,"total_committers":44,"mean_commits":6.863636363636363,"dds":0.4403973509933775,"last_synced_commit":"678e0edc58e056067476906f58b1654a0112f32f"},"previous_names":[],"tags_count":54,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fmenu","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fmenu/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fmenu/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fmenu/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spatie","download_url":"https://codeload.github.com/spatie/menu/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248813790,"owners_count":21165633,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["html","menus","php"],"created_at":"2024-08-01T13:02:11.996Z","updated_at":"2025-05-14T20:04:58.599Z","avatar_url":"https://github.com/spatie.png","language":"PHP","funding_links":["https://github.com/sponsors/spatie","https://spatie.be/open-source/support-us"],"categories":["Table of Contents","PHP","目录"],"sub_categories":["Navigation","导航 Navigation"],"readme":"\u003cdiv align=\"left\"\u003e\n    \u003ca href=\"https://spatie.be/open-source?utm_source=github\u0026utm_medium=banner\u0026utm_campaign=menu\"\u003e\n      \u003cpicture\u003e\n        \u003csource media=\"(prefers-color-scheme: dark)\" srcset=\"https://spatie.be/packages/header/menu/html/dark.webp?1\"\u003e\n        \u003cimg alt=\"Logo for menu\" src=\"https://spatie.be/packages/header/menu/html/light.webp?1\" height=\"190\"\u003e\n      \u003c/picture\u003e\n    \u003c/a\u003e\n\n\u003ch1\u003eFluent interface to build HTML menus in PHP\u003c/h1\u003e\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/menu.svg?style=flat-square)](https://packagist.org/packages/spatie/menu)\n[![Tests](https://github.com/spatie/menu/workflows/Tests/badge.svg)](https://github.com/spatie/menu/actions?query=workflow%3ATests+branch%3Amaster)\n[![Total Downloads](https://img.shields.io/packagist/dt/spatie/menu.svg?style=flat-square)](https://packagist.org/packages/spatie/menu)\n\n\u003c/div\u003e\n\nThe `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.\n\nIf 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.\n\nDocumentation is available at https://docs.spatie.be/menu.\n\nUpgrading from version 1? There's a [guide](https://github.com/spatie/menu#upgrading-to-20) for that!\n\n## Support us\n\n[\u003cimg src=\"https://github-ads.s3.eu-central-1.amazonaws.com/menu.jpg?t=1\" width=\"419px\" /\u003e](https://spatie.be/github-ad-click/menu)\n\nWe 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).\n\nWe 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).\n\n## Human Readable, Fluent Interface\n\nAll 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.\n\n```php\nMenu::new()\n    -\u003eadd(Link::to('/', 'Home'))\n    -\u003eadd(Link::to('/about', 'About'))\n    -\u003eadd(Link::to('/contact', 'Contact'))\n    -\u003eadd(Html::empty())\n    -\u003erender();\n\n// Or just...\nMenu::new()\n    -\u003elink('/', 'Home')\n    -\u003elink('/about', 'About')\n    -\u003elink('/contact', 'Contact')\n    -\u003eempty()\n```\n\n```html\n\u003cul\u003e\n    \u003cli\u003e\u003ca href=\"/\"\u003eHome\u003c/a\u003e\u003c/li\u003e\n    \u003cli\u003e\u003ca href=\"/about\"\u003eAbout\u003c/a\u003e\u003c/li\u003e\n    \u003cli\u003e\u003ca href=\"/contact\"\u003eContact\u003c/a\u003e\u003c/li\u003e\n    \u003cli\u003e\u003c/li\u003e\n\u003c/ul\u003e\n```\n\n## Or a More Programmatic Approach\n\nMenus can also be created through a reduce-like callable.\n\n```php\n$pages = [\n    '/' =\u003e 'Home',\n    '/about' =\u003e 'About',\n    '/contact' =\u003e 'Contact',\n];\n\nMenu::build($pages, function ($menu, $label, $url) {\n    $menu-\u003eadd($url, $label);\n})-\u003erender();\n```\n\n```html\n\u003cul\u003e\n    \u003cli\u003e\u003ca href=\"/\"\u003eHome\u003c/a\u003e\u003c/li\u003e\n    \u003cli\u003e\u003ca href=\"/about\"\u003eAbout\u003c/a\u003e\u003c/li\u003e\n    \u003cli\u003e\u003ca href=\"/contact\"\u003eContact\u003c/a\u003e\u003c/li\u003e\n\u003c/ul\u003e\n```\n\n## Strong Control Over the Html Output\n\nYou can programatically add html classes and attributes to any item in the menu, or to the menu itself.\n\n```php\nMenu::new()\n    -\u003eaddClass('navigation')\n    -\u003eadd(Link::to('/', 'Home')-\u003eaddClass('home-link'))\n    -\u003eadd(Link::to('/about', 'About'))\n    -\u003eadd(Link::to('/contact', 'Contact')-\u003eaddParentClass('float-right'))\n    -\u003ewrap('div.wrapper')\n```\n\n```html\n\u003cdiv class=\"wrapper\"\u003e\n    \u003cul class=\"navigation\"\u003e\n        \u003cli\u003e\u003ca href=\"/\" class=\"home-link\"\u003eHome\u003c/a\u003e\u003c/li\u003e\n        \u003cli\u003e\u003ca href=\"/about\"\u003eAbout\u003c/a\u003e\u003c/li\u003e\n        \u003cli class=\"float-right\"\u003e\u003ca href=\"/contact\"\u003eContact\u003c/a\u003e\u003c/li\u003e\n    \u003c/ul\u003e\n\u003c/div\n```\n\n## Adding id to elements\n\nYou can add id, so you can easily target some of these elements with CSS or JS.\n\n```php\nMenu::new()\n    -\u003eid('navigation')\n    -\u003eadd(Link::to('/', 'Home')-\u003eid('home-link'))\n    -\u003eadd(Link::to('/about', 'About'))\n    -\u003eadd(Link::to('/contact', 'Contact'))\n```\n\n```html\n\u003cul id=\"navigation\"\u003e\n    \u003cli\u003e\u003ca href=\"/\" id=\"home-link\"\u003eHome\u003c/a\u003e\u003c/li\u003e\n    \u003cli\u003e\u003ca href=\"/about\"\u003eAbout\u003c/a\u003e\u003c/li\u003e\n    \u003cli\u003e\u003ca href=\"/contact\"\u003eContact\u003c/a\u003e\u003c/li\u003e\n\u003c/ul\u003e\n```\n\n## Not Afraid of Depths\n\nThe menu supports submenus, which in turn can be nested infinitely.\n\n```php\nMenu::new()\n    -\u003elink('/', 'Home')\n    -\u003esubmenu('More', Menu::new()\n        -\u003eaddClass('submenu')\n        -\u003elink('/about', 'About')\n        -\u003elink('/contact', 'Contact')\n    );\n```\n\n```html\n\u003cul\u003e\n    \u003cli\u003e\u003ca href=\"/\"\u003eHome\u003c/a\u003e\u003c/li\u003e\n    \u003cli\u003e\n        More\n        \u003cul class=\"submenu\"\u003e\n            \u003cli\u003e\u003ca href=\"/about\"\u003eAbout\u003c/a\u003e\u003c/li\u003e\n            \u003cli\u003e\u003ca href=\"/contact\"\u003eContact\u003c/a\u003e\u003c/li\u003e\n        \u003c/ul\u003e\n    \u003c/li\u003e\n\u003c/ul\u003e\n```\n\n## Some Extra Treats for Laravel Apps\n\nThe Laravel version of the menu package adds some extras like convenience methods for generating URLs and macros.\n\n```php\nMenu::macro('main', function () {\n    return Menu::new()\n        -\u003eaction('HomeController@index', 'Home')\n        -\u003eaction('AboutController@index', 'About')\n        -\u003eaction('ContactController@index', 'Contact')\n        -\u003esetActiveFromRequest();\n});\n```\n\n```html\n\u003cnav class=\"navigation\"\u003e\n    {{ Menu::main() }}\n\u003c/nav\u003e\n```\n\nSpatie 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).\n\n## Install\n\nYou can install the package via composer:\n\n``` bash\ncomposer require spatie/menu\n```\n\n## Usage\n\nDocumentation is available at https://docs.spatie.be/menu.\n\n## Upgrading to 2.0\n\nUpgrading to 2.0 should be pretty painless for most use cases.\n\n### If you're just building menus...\n\n- The `void` and `voidIf` have been removed. These can be replaced by `html` and `htmlIf`, with empty strings as their first arguments\n- 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`.\n\n### If you're using custom `Item` implementations...\n\n- The `HtmlAttributes` and `ParentAttributes` traits have been renamed to `HasHtmlAttributes` and `HasParentAttributes`.\n- The `HasUrl` interface and trait has been removed. Url-related methods now also are part of the `Activatable` interface and trait.\n\n### New features...\n\n- Added the static `Menu::build` and non-static `Menu::fill` methods to create menu's from arrays.\n- The `setActive` method on `Activatable` now also accepts a non-strict boolean or callable parameter to set `$active` to true or false.\n- `Menu::html` and `Menu::htmlIf` now accept a `$parentAttributes` array as their second arguments.\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.\n\n## Testing\n\n``` bash\nphpunit\n```\n\n## Contributing\n\nPlease see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.\n\n## Security\n\nIf you've found a bug regarding security please mail [security@spatie.be](mailto:security@spatie.be) instead of using the issue tracker.\n\n## Postcardware\n\nYou'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.\n\nOur address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.\n\nWe publish all received postcards [on our company website](https://spatie.be/en/opensource/postcards).\n\n## Credits\n\n- [Sebastian De Deyne](https://github.com/sebastiandedeyne)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Fmenu","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspatie%2Fmenu","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Fmenu/lists"}