awes-io/navigator

🧿 为 Laravel 和 Awes.io 构建导航或菜单。菜单的复杂度和深度无限制。

v1.0.0 2019-07-31 08:13 UTC

This package is auto-updated.

Last update: 2024-08-29 05:27:14 UTC


README

Awes.io logo

导航器

Laravel 包,可以轻松创建任何复杂性的导航菜单。支持路由、权限、排序、渲染深度、活动项标记和元素搜索。

Coverage report Last version Build status Downloads License CDN Ready laravel Last commit Analytics Hosted by Package Kit Patreon

Navigator Laravel

目录

安装

通过 Composer

$ composer require awes-io/navigator

该包将自动注册自己。

快速入门

让我们首先创建基本的导航,这涵盖了大多数用例。

创建导航配置文件

// config/navigation.php

return [
    [
        'name' => 'Projects',
        'route' => 'projects.index', // route must exist or item will be hidden
        'children' => 
        [
            [
                'name' => 'New projects',
                'link' => '/projects/new', // you can use direct links
            ]
        ]
    ],
    [
        'name' => 'Packages',
        'route' => 'packages.index',
    ]
];

接下来,让我们在控制器中构建菜单,并将其传递到视图中

$menu = buildMenu(config('navigation'));
return view('menu', compact('menu'));

最后,实现基本的渲染逻辑

// menu.blade.php
@foreach($menu as $item)
  <ul>
    <li>
        @if($item->link())
            <a href="{{$item->link()}}">
              @if($item->isActive()) ACTIVE @endif {{$item->name}}
            </a>
        @else
            {{$item->name}}
        @endif
    </li>
    @if($item->hasChildren())
       @include('menu', ['menu' => $item->children()])
    @endif
  </ul>
@endforeach

就这么简单!

配置

您可以将配置文件发布

php artisan vendor:publish --provider="AwesIO\Navigator\NavigatorServiceProvider" --tag="config"

并将任何选项键重命名,这些键用于从菜单配置中获取相应的数据

// navigator.php config
'keys' => [
    'depth' => 'depth', // rendering depth
    'order' => 'order', // ordering by parameter
    'children' => 'children', // sub menu items
    'route' => 'route', // route name
    'link' => 'link', // item link url
    'title' => 'name', // item title
    'attr' => 'attr', // additional item attributes
],

以及使用替代菜单设置进行解析和渲染

// navigator.php config
'keys' => [
    ...
    'children' => 'other-children', // alternative sub menu items
    ...
],

// navigation.php
'menu' => [
    [
        ...
        'children' => [
        ...
        'other-children' => [
        ...
]

Navigator::buildMenu(config('navigation.menu')); // will now parse menu using 'other-children'

您可以通过上述映射动态地实现相同的效果

$menu = buildMenu(config('navigation.menu'), [], ['children' => 'other-children']);

请注意,我们现在使用全局辅助方法 buildMenu()

用法

use AwesIO\Navigator\Facades\Navigator;

$menu = Navigator::buildMenu(config('navigation.menu'), ['depth' => 2], [], function ($item) {
    $item->put('meta', $item->get('title') . ' / ' . $item->get('link'));
});

// using helper, rendering depth set via config as a second parameter
$menu = buildMenu(config('navigation.menu'), ['depth' => 2], [], function ($item) {});

第一个参数是数组形式的菜单配置

// navigation.php
return [
    'menu' => [
        [
            'title' => 'Products', // menu item's title
            'route' => 'products.index', // route name for URL generation
            'order' => 1, // parameter to determine the order
            'depth' => 1, // depth for the recursive generation of descendants
            'children' => 
            [
                [
                    'id' => 1, // custom id which overwrites auto-generated one
                    'title' => 'Catalog',
                    'link' => 'products/catalog', // explicit relative path for link URL 
                ],
                [
                    'title' => 'Support',
                    'route' => 'products.support'
                ]
            ]
        ],
        [
            'title' => 'Contact us',
            'route' => 'contacts',
            'order' => 2,
        ],
    ]
];

第二个是配置,第三个是配置参数的映射(如上所述),最后是回调,它将应用于每个菜单项。

一些有用的方法

确定节点是否有子节点并获取它们

$menu->hasChildren();
$menu->children();

获取节点的链接 URL

$menu->link();

确定节点是否当前选中并处于活动状态

$menu->isActive();

获取当前活动节点及其 ID

$menu->getActive();
$menu->getActiveId();

通过其 ID 查找节点

$menu->findById($id);

菜单渲染示例

// somewhere in a controller
$menu = Navigator::buildMenu(config('navigation.menu'));
return view('view', compact('menu'));

// view.blade.php
@extends('main')

@section('content')
    @include('menu', ['menu' => $menu])
@endsection

// menu.blade.php
@foreach($menu as $item)
  <ul>
    <li>
        @if($item->link())
            <a href="{{$item->link()}}">{{$item->title}}</a>
        @else
            {{$item->title}}
        @endif
    </li>
    @if($item->hasChildren())
       @include('menu', ['menu' => $item->children()])
    @endif
  </ul>
@endforeach

权限

如果用户没有权限访问某些菜单路由,它们将根据现有权限自动隐藏

Route::group(['middleware' => ['can:manage users']], function () {
    Route::get('/', 'RoleController@index')->name('admin.roles.index');
});

// will be excluded from the menu for non-admin users
[
    'name' => __('navigation.security'),
    'icon' => 'twousers',
    'route' => 'admin.roles.index',
],

测试

该包的覆盖率 覆盖率报告

您可以使用以下命令运行测试

composer test

贡献

请参阅 contributing.md 获取详细信息和一个待办事项列表。

致谢

许可协议

MIT