kraenkvisuell/nova-menu-builder

此Laravel Nova包允许您创建和管理菜单及菜单项。

v8.0.2 2022-04-06 15:07 UTC

README

Latest Version on Packagist Total Downloads

Laravel Nova包允许您创建和管理菜单及菜单项。

要求

  • php: >=7.2
  • laravel/nova: ^3.0

功能

  • 菜单管理
  • 菜单项管理
    • 简单的拖放嵌套和重新排序
  • 支持自定义菜单项类型
    • 轻松添加选择类型
    • 轻松添加自定义字段
    • 使用menubuilder:type命令轻松创建新类型
  • 完全本地化

屏幕截图

Menu Detail View

Menu Item Edit

安装和设置

安装包

通过Composer在Laravel Nova项目中安装包,编辑配置文件并运行迁移。

# Install the package
composer require optimistdigital/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

NovaServiceProvidertools()方法中注册工具

// in app/Providers/NovaServiceProvider.php

public function tools()
{
    return [
        // ...
        new \KraenkVisuell\MenuBuilder\MenuBuilder,
    ];
}

设置

发布配置文件后,您需要在配置中做一些必要的更改

# 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_US' => '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',
  // ...
];

自定义菜单项类型

菜单构建器允许您创建带有自定义字段的自定义菜单项类型。

创建一个扩展KraenkVisuell\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 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);
}

nova_get_menu_by_slug($menuSlug, $locale = null)

要获取单个菜单,您可以使用辅助函数nova_get_menu_by_slug('slug', 'en')。如果没有找到具有该别名的菜单,则返回null;如果找到了,则返回该菜单。如果没有传递区域设置,则辅助函数将自动选择第一个配置的区域设置。

鸣谢

许可协议

Nova Menu Builder是开源软件,受MIT许可协议许可。