markrassamni/nova-resource-landing-page

决定资源是否应打开在索引、创建、更新或详情页面

v3.0.0 2020-10-31 05:26 UTC

This package is auto-updated.

Last update: 2024-08-29 05:00:26 UTC


README

License Latest Version on Packagist Total Downloads

添加了创建直接导航到资源详情、创建或编辑页面的功能。

先决条件

安装

$ composer require markrassamni/nova-resource-landing-page

修改 app/Nova/Resource.php 以实现 ResourceLandingPageInterface 接口和 SupportChangeResourceLandingPage 特性

<?php

namespace App\Nova;

use MarkRassamni\NovaResourceLandingPage\Traits\SupportChangeResourceLandingPage;
use MarkRassamni\NovaResourceLandingPage\Contracts\ResourceLandingPageInterface;
use Laravel\Nova\Resource as NovaResource;

abstract class Resource extends NovaResource implements ResourceLandingPageInterface
{
    use SupportChangeResourceLandingPage;

    ...
}

发布资源

$ php artisan vendor:publish --provider="MarkRassamni\NovaResourceLandingPage\Providers\NovaResourceLandingPageServiceProvider"

更新

在更新时,重要的一点是重新发布资源,如下所示

$ php artisan vendor:publish --force --provider="MarkRassamni\NovaResourceLandingPage\Providers\NovaResourceLandingPageServiceProvider"

卸载

从composer中移除

$ composer remove markrassamni/nova-resource-landing-page

从您的Nova资源中移除 SupportChangeResourceLandingPage 特性

use SupportChangeResourceLandingPage;

移除自定义导航模板

rm resources/views/vendor/nova/resources/navigation.blade.php

使用方法

在应该导航到详情页面的模型上放置以下方法。

class MyResource extends Resource
{
    public static function detail(): bool
    {
        return true;
    }
}

在应该导航到创建页面的模型上放置以下方法。

class MyResource extends Resource
{
    public static function create(): bool
    {
        return true;
    }
}

在应该导航到编辑页面的模型上放置以下方法。

class MyResource extends Resource
{
    public static function edit(): bool
    {
        return true;
    }
}

可选地覆盖资源标识符以在打开详情或编辑页面时使用。

class MyResource extends Resource
{
    /**
     * @return string|int
     */
    public static function recordId()
    {
        return 1;
    }
}

工作原理

Laravel Nova 有能力覆盖用于渲染导航侧边栏的 Blade 模板。模板是从 Nova 版本 v3.4.1 复制并经过几行修改以支持直接链接资源到详情视图。当使用带有 nova-views 标签发布供应商资源时,模板将被放置在项目的 resources/views/vendor/nova/resources 文件夹中。

视图更改
@if ($resource::detail())
    <router-link :to="{
        name: 'detail',
        params: {
            resourceName: '{{ $resource::uriKey() }}',
            resourceId: {{ $resource::recordId() }}
        }
    }" class="text-white text-justify no-underline dim">
        {{ $resource::label() }}
    </router-link>
@elseif ($resource::create())
    <router-link :to="{
        name: 'create',
        params: {
            resourceName: '{{ $resource::uriKey() }}',
        }
    }" class="text-white text-justify no-underline dim">
        {{ $resource::label() }}
    </router-link>
@elseif ($resource::edit())
    <router-link :to="{
        name: 'edit',
        params: {
            resourceName: '{{ $resource::uriKey() }}',
            resourceId: {{ $resource::recordId() }}
        }
    }" class="text-white text-justify no-underline dim">
        {{ $resource::label() }}
    </router-link>
@else
    <router-link :to="{
        name: 'index',
        params: {
            resourceName: '{{ $resource::uriKey() }}'
        }
    }" class="text-white text-justify no-underline dim">
        {{ $resource::label() }}
    </router-link>
@endif