red-freak/laravel-menu
Composer 安装命令:
composer require red-freak/laravel-menu
包简介
A Laravel Facade to generate Menus.
README 文档
README
Laravel Menu is a package to create menus for laravel applications. It's a package to also solve the problem for module based applications (see red-freak/laravel-modules).
Installation
Simply install the package via composer:
composer require red-freak/laravel-menu
How to use
Basic usage
The base idea is to be able to create or add items via a Menu-Facade. It can be accessed from anywhere. So you are able to have different (e.g. module-bases) ServiceProviders to do so.
Menu::add('home') ->add(new Item('Home', '/')) ->add(new Item('About', '/about')) ->add(new Item('Login', '/login'));
<ul class="menu menu-level-0"> <li class="menu-item menu-level-1"> <a href="/">Home</a> </li> <li class="menu-item menu-level-1"> <a href="/about">About</a> </li> <li class="menu-item menu-level-1"> <a href="/login">Login</a> </li> </ul>
Add items to a menu
You can define the menus in the config file for later use in your project and can also add items to a menu via the Menu-Facade. And of course it can work with labels.
// define the menu anywhere to use translation keys as labels (or do it by config) Menu::add('home', [ RenderOptions::KEY_USE_LABELS_AS_TRANSLATION_KEYS => true ]); ... // have the corresponding translation keys via files (or in this case via the `Translator`-Facade) app()->translator->addLines([ 'menu.label.home' => 'Home', 'menu.label.about' => 'About', 'menu.label.login' => 'Login', ], 'en'); ... // Now you can add items via the `Item` class ... Menu::get('home') ->add(new Item('menu.label.home', 'http://localhost')) ->add(new Item('menu.label.about', 'http://localhost/about')) ->add(new Item('menu.label.login', 'http://localhost/login')); // ... or add them via key-value ... Menu::get('home') ->add([ Menu::KEY_ITEM_LABEL => 'Home', Menu::KEY_ITEM_LINK => 'http://localhost', ]) ->add([ Menu::KEY_ITEM_LABEL => 'About', Menu::KEY_ITEM_LINK => 'http://localhost/about', ]) ->add([ Menu::KEY_ITEM_LABEL => 'Login', Menu::KEY_ITEM_LINK => 'http://localhost/login', ]); // ... or add them via routes ... // Route::get('/', fn() => 'home')->name('home'); // Route::get('/about', fn() => 'about')->name('about'); // Route::get('/login', fn() => 'login')->name('login'); Menu::get('home') ->add([Menu::KEY_ITEM_ROUTE => 'home']) ->add([Menu::KEY_ITEM_ROUTE => 'about']) ->add([Menu::KEY_ITEM_ROUTE => 'login']);
If you now call Menu::home() via the automatically create macro, you will get the following result:
<ul class="menu menu-level-0"> <li class="menu-item menu-level-1"> <a href="http://localhost">Home</a> </li> <li class="menu-item menu-level-1"> <a href="http://localhost/about">About</a> </li> <li class="menu-item menu-level-1"> <a href="http://localhost/login">Login</a> </li> </ul>
Add a submenu for a Model
// define the route and the translation keys Route::resource('users', Controller::class); app()->translator->addLines([ 'menu.label.users.index' => 'Users', 'menu.label.users.create' => 'create User', ], 'en');
// define the menu and add a submenu by a model Menu::add('home', [RenderOptions::KEY_USE_LABELS_AS_TRANSLATION_KEYS => true])->add([ Menu::KEY_ITEM_MODEL => User::class, ]);
If you now call Menu::home() via the automatically create macro, you will get the following result:
<ul class="menu menu-level-0"> <li class="sub-menu menu-item menu-level-1"> Users <ul class="sub-menu menu-item menu-level-2"> <li class="menu-item menu-level-3"> <a href="http://localhost/users">Users</a> </li> <li class="menu-item menu-level-3"> <a href="http://localhost/users/create">create User</a> </li> </ul> </li> </ul>
run tests
vendor/bin/pest --coverage-html ./tests/reports/pest
special thanks
- Spatie for general inspiration on package development and their workflows
red-freak/laravel-menu 适用场景与选型建议
red-freak/laravel-menu 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 10 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 red-freak/laravel-menu 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 red-freak/laravel-menu 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-10-06