marshmallow / nova-4-menu-builder
这个Laravel Nova包允许您创建和管理菜单及菜单项。
v7.1.9
2024-08-26 15:46 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
README
此Laravel Nova包允许您创建和管理菜单及菜单项。
要求
php: >=8.0
laravel/nova: ^4.0
特性
- 菜单管理
- 菜单项管理
- 简单的拖放嵌套和重新排序
- 支持自定义菜单项类型
- 能够轻松添加选择类型
- 能够添加自定义字段
- 使用
menubuilder:type
命令轻松创建新类型
- 完全本地化
屏幕截图
安装和设置
安装包
通过Composer在Laravel Nova项目中安装包,编辑配置文件并运行迁移。
# Install the package composer require marshmallow/nova-4-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 [ // ... \Marshmallow\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', // ... ];
自定义菜单项类型
菜单构建器允许您使用自定义字段创建自定义菜单项类型。
创建一个扩展Marshmallow\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许可证。