插件可以通过覆盖 插件注册文件 的 registerNavigation 方法来扩展后台导航菜单。本节将向您展示如何向后台导航区域添加菜单项。以下是一个注册具有两个子菜单项的顶级导航菜单项的示例。
public function registerNavigation()
{
return [
'blog' => [
'label' => 'Blog',
'url' => Backend::url('acme/blog/posts'),
'icon' => 'icon-pencil',
'permissions' => ['acme.blog.*'],
'order' => 500,
'sideMenu' => [
'posts' => [
'label' => 'Posts',
'icon' => 'icon-copy',
'url' => Backend::url('acme/blog/posts'),
'permissions' => ['acme.blog.access_posts'],
],
'categories' => [
'label' => 'Categories',
'icon' => 'icon-copy',
'url' => Backend::url('acme/blog/categories'),
'permissions' => ['acme.blog.access_categories'],
]
]
]
];
}当您注册后端导航时,您可以使用 本地化字符串 作为 label 值。后端导航也可以通过 permissions 值控制,并与已定义的 后端用户权限 对应。后端导航在整体导航菜单项中出现的顺序,由 order 值控制。数字越大,表示该项在菜单项顺序中出现得越晚;而数字越小,则表示它出现得越早。
要使子菜单项可见,您可以在 后端控制器 中使用 BackendMenu::setContext 方法设置导航上下文。这将使父菜单项处于活动状态,并在侧边菜单中显示子项。
| Property | Description |
|---|---|
| label | specifies the menu label localization string key, required. |
| order | a numerical weight when determining the display order. |
| icon | an icon name from the October CMS icon collection, optional. |
| iconSvg | an SVG icon to be used in place of the standard icon, the SVG icon should be a rectangle and can support colors, optional. |
| url | the URL the menu item should point to (eg. Backend::url('author/plugin/controller/action'), required. |
| counter | a numeric value to output near the menu icon. The value should be a number or a callable returning a number, optional. |
| counterLabel | a string value to describe the numeric reference in counter, optional. |
| attributes | an associative array of attributes and values to apply to the menu item, optional. |
| permissions | an array of permissions the backend user must have in order to view the menu item (Note: direct access of URLs still requires separate permission checks), optional. |
| sideMenu | an array of sub-menu items sharing the same configuration as parent menu items, optional. |
| itemType | specifies a display type for the item, sub-menu items only. Supported: primary, link, ruler, section. Default: link. |
| visibleOn | used by sub-menu items, provides a list of other items, when active makes this item visible, optional. |
以下是系统生成的值,并且在注册导航项时不会提供。
| Key | Description |
|---|---|
| code | a string value that acts as an unique identifier for that menu option. |
| owner | a string value that specifies the menu items owner plugin or module in the format "Author.Plugin". |
导航项支持指定一个计数器,以指示有需要关注的项。这些属性可用于父级和子级菜单项。使用 计数器 和 计数器标签 来显示一个数字计数器。
'blog' => [
// ...
'counter' => [\Author\Plugin\Classes\MyMenuCounterService::class, 'getCounterMethod'],
'counterLabel' => 'Label describing a dynamic menu counter',
],子菜单项支持不同的显示类型,使用 itemType 属性,包括用户界面元素。将类型设置为 section 以显示一个导航部分。在键名中添加下划线前缀 (_) 是一种很好的方式来表明它是一个 UI 元素。
'_section1' => [
'itemType' => 'section',
'label' => 'Advanced',
],将一个部分与在其上方放置的导航分隔线结合起来可能很有用。将类型设置为 ruler 以显示分隔线。
'_ruler1' => [
'itemType' => 'ruler',
],要显示行动号召,请将类型设置为 primary,以将链接显示为主按钮。
'people_create' => [
'label' => 'New Person',
'icon' => 'icon-plus',
'url' => Backend::url('acme/blog/people/create'),
'itemType' => 'primary',
],它还可以用于提供多个 primary 链接,并使用 visibleOn 属性有条件地显示它们。下面将显示,当 人员 子菜单处于活动状态时,新建人员 按钮;同样地,对于 新建帖子 和 帖子 子菜单项也是如此。
'people_create' => [
'label' => 'New Person',
'icon' => 'icon-plus',
'url' => Backend::url('acme/blog/people/create'),
'itemType' => 'primary',
'visibleOn' => 'people',
],
'post_create' => [
'label' => 'New Post',
'icon' => 'icon-plus',
'url' => Backend::url('acme/blog/people/create'),
'itemType' => 'primary',
'visibleOn' => 'posts',
],该 backend.menu.extendItems 事件监听器 可用于在系统和插件注册其导航项后修改现有导航项。该事件返回一个导航 $manager 实例,支持以下方法。
| Method | Description |
|---|---|
| addMainMenuItems($owner, $definitions) | Add or update a main menu definition |
| getMainMenuItem($owner, $code) | Retrieve an existing main menu definition |
| removeMainMenuItem($owner, $code) | Delete an existing main menu definition |
| addSideMenuItems($owner, $code, $definitions) | Add or update a side menu definition |
| getSideMenuItem($owner, $code, $sideCode) | Retrieve an existing side menu definition |
| removeSideMenuItem($owner, $code, $sideCode) | Delete an existing side menu definition |
在检索菜单项时,该对象支持方法链来更新其属性。以下示例将把 Editor 的标签替换为 Code Editor。
Event::listen('backend.menu.extendItems', function($manager) {
$manager->getMainMenuItem('October.Editor', 'editor')->label('Code Editor');
});下一个示例将把标签更改为新闻并添加一个9计数器到 Acme 博客插件。
Event::listen('backend.menu.extendItems', function($manager) {
$manager->getMainMenuItem('Acme.Blog', 'blog')
->getSideMenuItem('posts')
->label('News')
->counter(9);
});同样地,我们可以使用相同的事件移除菜单项。以下示例将分别移除所有项、移除单个项以及移除多个项。
Event::listen('backend.menu.extendItems', function($manager) {
$manager->removeMainMenuItem('Acme.Blog', 'blog');
$manager->removeSideMenuItem('Acme.Blog', 'blog', 'posts');
$manager->removeSideMenuItems('Acme.Blog', 'blog', [
'posts',
'categories'
]);
});