optimistdigital / nova-menu-builder
此 Laravel Nova 包允许您创建和管理菜单及菜单项。
7.1.7
2024-09-09 10:10 UTC
Requires
- php: >=8.0
- doctrine/dbal: >=2.0.0
- laravel/nova: ^4.0
- outl1ne/nova-translations-loader: ^5.0.0
Requires (Dev)
- orchestra/testbench: ^7.0
- phpunit/phpunit: ^9.0
- dev-main
- 7.1.7
- 7.1.6
- 7.1.5
- 7.1.4
- 7.1.3
- 7.1.2
- 7.1.1
- 7.1.0
- 7.0.15
- 7.0.14
- 7.0.13
- 7.0.12
- 7.0.11
- 7.0.10
- 7.0.9
- 7.0.8
- 7.0.7
- 7.0.6
- 7.0.5
- 7.0.4
- 7.0.3
- 7.0.2
- 7.0.1
- 7.0.0
- 6.1.3
- 6.1.2
- 6.1.1
- 6.1.0
- 6.0.5
- 6.0.4
- 6.0.3
- 6.0.2
- 6.0.1
- 6.0.0
- 5.3.3
- 5.3.2
- 5.3.1
- 5.3.0
- 5.2.5
- 5.2.4
- 5.2.3
- 5.2.2
- 5.2.1
- 5.2.0
- 5.1.0
- 5.0.1
- 5.0.0
- 4.1.2
- 4.1.1
- 4.1.0
- 4.0.5
- 4.0.4
- 4.0.3
- 4.0.2
- 4.0.1
- 4.0.0
- 3.0.x-dev
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 2.3.9
- 2.3.8
- 2.3.7
- 2.3.6
- 2.3.5
- 2.3.4
- 2.3.3
- 2.3.2
- 2.3.1
- 2.3.0
- 2.2.3
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.5
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.x-dev
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.1
- 1.2.0
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- dev-dependabot/npm_and_yarn/ws-8.17.1
- dev-dependabot/npm_and_yarn/braces-3.0.3
- dev-dependabot/npm_and_yarn/express-4.19.2
- dev-dependabot/npm_and_yarn/webpack-dev-middleware-5.3.4
This package is auto-updated.
Last update: 2024-09-09 10:11:06 UTC
README
此 Laravel Nova 包允许您创建和管理菜单及菜单项。
要求
php: >=8.0
laravel/nova: ^4.0
特性
- 菜单管理
- 菜单项管理
- 简单拖放嵌套和重新排序
- 支持自定义菜单项类型
- 易于添加选择类型
- 可添加自定义字段
- 使用
menubuilder:type
命令轻松创建新类型
- 完全本地化
屏幕截图
安装和设置
安装包
使用 Composer 在 Laravel Nova 项目中安装此包,编辑配置文件并运行迁移。
# Install the package composer require outl1ne/nova-menu-builder # Publish the configuration file and edit it to your preference # NB! If you want custom table names, configure them before running the migrations. php artisan vendor:publish --tag=nova-menu-builder-config
在 NovaServiceProvider
的 tools()
方法中注册工具
// in app/Providers/NovaServiceProvider.php public function tools() { return [ // ... \Outl1ne\MenuBuilder\MenuBuilder::make(), // Optional customization ->title('Menus') // Define a new name for sidebar ->icon('adjustments') // Customize menu icon, supports heroicons ->hideMenu(false) // Hide MenuBuilder defined MenuSection. ]; }
设置
发布配置文件后,您需要在配置中进行一些必要的更改。
# Choose table names of your liking by editing the two key/values: 'menus_table_name' => 'nova_menu_menus', 'menu_items_table_name' => 'nova_menu_menu_items', # Define the locales for your project: # If your project doesn't have localization, you can just leave it as it is. # When there's just one locale, anything related to localization isn't displayed. 'locales' => ['en' => 'English'], # Define the list of possible menus (ie 'footer', 'header', 'main-menu'): 'menus' => [ // 'header' => [ // 'name' => 'Header', // 'unique' => true, // 'menu_item_types' => [] // ] ], # If you're just setting up, this is probably of no importance to you, # but later on, when you want custom menu item types with custom fields # , you can register them here: 'menu_item_types' => [],
接下来,只需运行迁移即可。
# Run the automatically loaded migrations
php artisan migrate
可选:发布迁移
这仅在你想要覆盖迁移和模型时有用。如果你希望使用菜单构建器作为默认设置,则不需要它们。
# Publish migrations to overwrite them (optional)
php artisan vendor:publish --tag=nova-menu-builder-migrations
使用方法
区域配置
您可以在配置文件中定义菜单的区域,如下所示。
// in config/nova-menu.php return [ // ... 'locales' => [ 'en' => 'English', 'et' => 'Estonian', ], // or using a closure (not cacheable): 'locales' => function() { return nova_lang_get_locales(); } // or if you want to use a function, but still be able to cache it: 'locales' => '\App\Configuration\NovaMenuConfiguration@getLocales', // or 'locales' => 'nova_lang_get_locales', // ... ];
自定义菜单项类型
菜单构建器允许您使用自定义字段创建自定义菜单项类型。
创建一个扩展 Outl1ne\MenuBuilder\MenuItemTypes\BaseMenuItemType
类的类并在配置文件中注册它。
// in config/nova-menu.php return [ // ... 'menu_item_types' => [ \App\MenuItemTypes\CustomMenuItemType::class, ], // ... ];
在创建的类中,重写以下方法
/** * Get the menu link identifier that can be used to tell different custom * links apart (ie 'page' or 'product'). * * @return string **/ public static function getIdentifier(): string { // Example usecase // return 'page'; return ''; } /** * Get menu link name shown in a dropdown in CMS when selecting link type * ie ('Product Link'). * * @return string **/ public static function getName(): string { // Example usecase // return 'Page Link'; return ''; } /** * Get list of options shown in a select dropdown. * * Should be a map of [key => value, ...], where key is a unique identifier * and value is the displayed string. * * @return array **/ public static function getOptions($locale): array { // Example usecase // return Page::all()->pluck('name', 'id')->toArray(); return []; } /** * Get the subtitle value shown in CMS menu items list. * * @param $value * @param $data The data from item fields. * @param $locale * @return string **/ public static function getDisplayValue($value, ?array $data, $locale) { // Example usecase // return 'Page: ' . Page::find($value)->name; return $value; } /** * Get the enabled value * * @param $value * @param $data The data from item fields. * @param $locale * @return string */ public static function getEnabledValue($value, ?array $data, $locale) { return true; } /** * Get the value of the link visible to the front-end. * * Can be anything. It is up to you how you will handle parsing it. * * This will only be called when using the nova_get_menu() * and nova_get_menus() helpers or when you call formatForAPI() * on the Menu model. * * @param $value The key from options list that was selected. * @param $data The data from item fields. * @param $locale * @return any */ public static function getValue($value, ?array $data, $locale) { return $value; } /** * Get the fields displayed by the resource. * * @return array An array of fields. */ public static function getFields(): array { return []; } /** * Get the rules for the resource. * * @return array A key-value map of attributes and rules. */ public static function getRules(): array { return []; } /** * Get data of the link visible to the front-end. * * Can be anything. It is up to you how you will handle parsing it. * * This will only be called when using the nova_get_menu() * and nova_get_menus() helpers or when you call formatForAPI() * on the Menu model. * * @param null $data Field values * @return any */ public static function getData($data = null) { return $data; }
以 JSON API 返回菜单
nova_get_menus()
此包中全局注册了一个辅助函数 nova_get_menus
,它以 API 友好格式返回所有菜单及其菜单项。
public function getMenus(Request $request) { $menusResponse = nova_get_menus(); return response()->json($menusResponse); }
通过标识符获取单个菜单
// Available helpers nova_get_menu_by_slug($menuSlug, $locale = null) nova_get_menu_by_id($menuId, $locale = null)
要获取单个菜单,可以使用可用的辅助函数。
如果没有找到具有该标识符的菜单,则返回 null,如果找到,则返回该菜单。如果没有传递区域,则辅助函数将自动选择第一个配置的区域。
鸣谢
许可
Nova Menu Builder 是开源软件,根据 MIT 许可证 许可。