spatie/menu
Composer 安装命令:
composer require spatie/menu
包简介
Html menu generator
关键字:
README 文档
README
Fluent interface to build HTML menus in PHP
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 provides some extra treats.
If you're looking for a more flexible and renderless solution, maybe our spiritual successor Laravel Navigation is what you're looking for.
Documentation is available at https://docs.spatie.be/menu.
Upgrading from version 1? There's a guide for that!
Support us
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
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. We publish all received postcards on our virtual postcard wall.
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()
<ul> <li><a href="/">Home</a></li> <li><a href="/about">About</a></li> <li><a href="/contact">Contact</a></li> <li></li> </ul>
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();
<ul> <li><a href="/">Home</a></li> <li><a href="/about">About</a></li> <li><a href="/contact">Contact</a></li> </ul>
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')
<div class="wrapper"> <ul class="navigation"> <li><a href="/" class="home-link">Home</a></li> <li><a href="/about">About</a></li> <li class="float-right"><a href="/contact">Contact</a></li> </ul> </div
Adding id to elements
You can add id, so you can easily target some of these elements with CSS or JS.
Menu::new() ->id('navigation') ->add(Link::to('/', 'Home')->id('home-link')) ->add(Link::to('/about', 'About')) ->add(Link::to('/contact', 'Contact'))
<ul id="navigation"> <li><a href="/" id="home-link">Home</a></li> <li><a href="/about">About</a></li> <li><a href="/contact">Contact</a></li> </ul>
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') );
<ul> <li><a href="/">Home</a></li> <li> More <ul class="submenu"> <li><a href="/about">About</a></li> <li><a href="/contact">Contact</a></li> </ul> </li> </ul>
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(); });
<nav class="navigation"> {{ Menu::main() }} </nav>
Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.
Install
You can install the package via composer:
composer require spatie/menu
Usage
Documentation is available at https://docs.spatie.be/menu.
Upgrading to 2.0
Upgrading to 2.0 should be pretty painless for most use cases.
If you're just building menus...
- The
voidandvoidIfhave been removed. These can be replaced byhtmlandhtmlIf, with empty strings as their first arguments - The
prefixLinksandprefixUrlsmethods 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 withapplyToAll.
If you're using custom Item implementations...
- The
HtmlAttributesandParentAttributestraits have been renamed toHasHtmlAttributesandHasParentAttributes. - The
HasUrlinterface and trait has been removed. Url-related methods now also are part of theActivatableinterface and trait.
New features...
- Added the static
Menu::buildand non-staticMenu::fillmethods to create menu's from arrays. - The
setActivemethod onActivatablenow also accepts a non-strict boolean or callable parameter to set$activeto true or false. Menu::htmlandMenu::htmlIfnow accept a$parentAttributesarray as their second arguments.
Changelog
Please see CHANGELOG for more information what has changed recently.
Testing
phpunit
Contributing
Please see CONTRIBUTING for details.
Security
If you've found a bug regarding security please mail security@spatie.be instead of using the issue tracker.
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.
Credits
License
The MIT License (MIT). Please see License File for more information.
spatie/menu 适用场景与选型建议
spatie/menu 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.06M 次下载、GitHub Stars 达 758, 最近一次更新时间为 2016 年 02 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「menu」 「navigation」 「spatie」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 spatie/menu 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 spatie/menu 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 spatie/menu 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A block to display a list of links to child pages, or pages in current level
A Symfony extension to get active class base on current bundle/controller/action
Receive webhooks in Laravel apps
Easily optimize images using PHP
Store your application settings
Laravel Menu management
统计信息
- 总下载量: 3.06M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 767
- 点击次数: 13
- 依赖项目数: 11
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-02-16