dewsign / nova-navigation
Laravel Nova 的导航/菜单管理器
Requires
- php: >=7.0.0
- benjaminhirsch/nova-slug-field: ^1.0
- dewsign/nova-repeater-blocks: ^1.3.3
- epartment/nova-dependency-container: ^1.1
- laravel/framework: >=5.0.0
- maxfactor/support: ^2.0.0
- silvanite/brandenburg: ^1.0
- silvanite/nova-field-cloudinary: ^1.0
- silvanite/nova-field-hidden: ^1.0
Requires (Dev)
- squizlabs/php_codesniffer: >=3.1
This package is auto-updated.
Last update: 2024-09-07 20:42:26 UTC
README
安装
composer require dewsign/nova-navigation
php artisan migrate
使用
要开始,您需要创建第一个导航区域。例如,页眉或页脚导航。您可以根据自己的喜好结构化文件,或者如果您更喜欢,可以使用示例中的约定。
我们简单地使用默认的 Nova 文件夹来注册新的导航,因为它将自动加载。
// app/Nova/HeaderNavigation.php namespace App\Nova; use Dewsign\NovaNavigation\Nova\Navigation; class HeaderNavigation extends Navigation { public static $zone = 'header'; public static function label() { return __('Header Links'); } }
$zone 用于在数据库和代码中区分不同的导航区域,避免了为每个新的导航区域需要新的表。
您的新导航区域现在将在 Nova 中可用,默认的链接项是唯一选项。
输出导航(Blade)
导航 Blade 指令
包含了一个 blade 指令,主要用于通过视图渲染导航区域。包含了一个默认的无序列表视图,可以从中发布。
在您的 blade 视图模板或布局中 ...
@novaNavigation('my-zone')
这将通过默认视图递归地渲染所有导航项。要创建自定义视图,您可以在 resources/views/vendor/nova-navigation/zone//my-zone.blade.php 中创建一个新的 blade 视图。
您可以使用默认视图作为模板。
<ul>
@foreach ($items as $item)
<li>
{!! $item->view !!}
@if ($item->navigations->count())
@novaNavigation($zone, $item->navigations)
@endif
</li>
@endforeach
</ul>
您会注意到它为父项嵌套的每个级别的导航渲染相同的视图。如果您不希望这样,您将需要手动遍历子项。您可以通过 navigations 关系访问任何子项。
@foreach($navigationItem->navigations as $item) {!! $item->view !!} @endforeach
view 属性渲染分配给导航项类型的 blade 视图。默认的定制项视图可以通过供应商视图 resources/views/vendor/nova-navigation/custom.blade.php 发布和修改。
如果您不希望渲染导航项的默认视图,您可以手动构建内联视图并使用 label 和 action 属性。
<a href="{{ $navigationItem->action }}">{{ $navigationItem->label }}</a>
链接类型
该包尝试确定输入的链接类型,并通过 linkType 属性在项上返回此信息。您可以在 view 中使用 $item->linkType 访问它。这可以用于为链接添加图标等。
扩展
您可以通过创建一些新文件并加载它们来创建自己的导航项类型。这很有用,可以将内容类型作为可选择的导航项提供,而不是手动输入自定义 URL。例如,如果您有一个博客,并希望用户能够选择文章或类别进行链接。
您需要
- 一个完整的 Eloquent 模型,包括迁移
- 一个 Nova 资源来管理内容
- 一个用于渲染项的 blade 视图
// app/Navigation/Models/Category.php use Dewsign\NovaNavigation\Models\NavigationItem; class Section extends NavigationItem { public static $viewTemplate = 'nova-navigation::category'; public function category() { // BlogCategoryModel is used as an example. Include your actual blog category model. return $this->belongsTo(BlogCategoryModel::class); } // Return the url for this navigation item public function resolveAction() { return route('blog.category', $this->category); } // Return the label to display in the navigation public function resolveLabel($category = null) { // Automatically use the category title as navigation label return $category->title; } }
// database/migrations/your_migration.php Schema::create('nav_categories', function (Blueprint $table) { $table->increments('id'); $table->integer('category_id')->nullable(); $table->timestamps(); });
// app/Navigation/Nova/Category.php ... use Dewsign\NovaNavigation\Nova\Items\NavigationItem; class Section extends NavigationItem { // The model we just created public static $model = App\Navigation\Models\Category::class; public static function label() { return __('Category'); } public function fields(Request $request) { return [ // BlogCategoryResource is used as an example. Include your actual blog category nova resource. BelongsTo::make('Category', 'category', BlogCategoryResource::class)->searchable(), ]; } }
接下来,您需要告诉系统如何渲染分类导航项。在模型中创建一个新视图(在这个示例中 resources/views/vendor/nova-navigation/category.blade.php)。或者,如果您只想为所有导航项渲染相同的布局,可以将模型中的视图设置为默认的 nova-navigation::custom。
<a href="{{ $model->action }}" class="category-navigation-item">{{ $model->label }}</a>
$model 引用我们创建的类别模型。如果您想在视图中包含更多详细信息,可以引用关系 $model->category。例如。
<a href="{{ $model->action }}" class="category-navigation-item">
{{ $model->label }} ({{ $model->category->articles()->count() }})
</a>
最后,通过 novanavigation 配置加载新的导航项
return [ "repeaters" => [ \App\Navigation\Nova\Category::class, ], "replaceRepeaters" => false, ];
用户现在可以添加一个 分类 导航项,并选择一个现有分类进行链接。好处是,当分类更改时,导航项也会相应反映这些变化。
超链接重复块
如果您正在使用 Nova Repeater Blocks 来构建内容,您可能希望包含一个重复块,该块类似于导航区域以添加内联链接。为您的重复块添加包含的 NrbHyperlinks。
// config/repeater-blocks ... "repeaters" => [ ... Dewsign\NovaNavigation\Nova\NrbHyperlinks::class, ],
这将使用各自的视图渲染每个导航项。可以通过在 views/vendor/nova-navigation/hyperlinks 目录中创建新模板来自定义每个项目的包装器。如有需要,您可以发布和修改默认设置。