illuminatech/nova-config

A Laravel Nova工具,用于应用程序配置管理。

2.0.0 2023-03-14 11:46 UTC

This package is auto-updated.

Last update: 2024-09-14 15:11:21 UTC


README

A Laravel Nova工具,用于应用程序配置管理


此扩展提供Laravel Nova Web界面,用于应用程序配置设置。

有关许可信息,请参阅LICENSE文件。

Latest Stable Version Total Downloads

安装

安装此扩展的首选方式是通过composer

运行以下命令之一:

php composer.phar require --prefer-dist illuminatech/nova-config

或者

"illuminatech/nova-config": "*"

将其添加到你的composer.json文件的require部分。

用法

此扩展提供Laravel Nova Web界面,用于应用程序配置设置。它提供了一个用于配置参数设置和恢复默认值的单一表单。

此扩展依赖于illuminatech/config包进行实际的配置管理。在尝试使用此包之前,请确保您熟悉illuminatech/config包。

首先,您需要为您的应用程序设置持久配置,指定从Nova管理面板可编辑的特定配置项。例如

<?php

namespace App\Providers;

use Illuminatech\Config\Providers\AbstractPersistentConfigServiceProvider;

class PersistentConfigServiceProvider extends AbstractPersistentConfigServiceProvider
{
    protected function items(): array
    {
        return [
            'app.name' => [
                'label' => __('Application name'),
                'rules' => ['sometimes', 'required'],
            ],
            'app.debug' => [
                'label' => __('Debug mode enabled'),
                'rules' => ['sometimes', 'required', 'boolean'],
                'cast' => 'boolean',
            ],
            'app.env' => [
                'label' => __('Application environment'),
                'rules' => ['sometimes', 'required'],
            ],
            // ...
        ];
    }

    // ...
}

不要忘记在"config/app.php"的"providers"部分注册您的持久配置服务提供者

<?php

return [
    // ...
    'providers' => [
        // ...
        App\Providers\PersistentConfigServiceProvider::class,
    ],
    // ...
];

接下来,您应该在您的NovaServiceProvider中注册\Illuminatech\NovaConfig\NovaConfig工具

<?php

namespace App\Providers;

use Laravel\Nova\NovaApplicationServiceProvider;

class NovaServiceProvider extends NovaApplicationServiceProvider
{
    public function tools()
    {
        return [
            new \Illuminatech\NovaConfig\NovaConfig(),
            // ...
        ];
    }
}

完成操作后,新的工具“设置”将出现在Nova侧边栏菜单中,引导至配置设置表单。

字段配置

用于通过\Illuminatech\Config\Item::$options定义的特定配置项管理的表单字段。默认情况下,将使用常规文本输入。如果\Illuminatech\Config\Item::$cast设置为"bool"或"boolean",则将使用复选框字段。如果提供了'选项'数组,则将渲染下拉(例如选择)输入。您可以使用'component'键手动定义要使用的确切Nova字段。例如

<?php

namespace App\Providers;

use Illuminatech\Config\Providers\AbstractPersistentConfigServiceProvider;

class PersistentConfigServiceProvider extends AbstractPersistentConfigServiceProvider
{
    protected function items(): array
    {
        return [
            'app.name' => [
                'label' => __('Application name'),
                'rules' => ['sometimes', 'required'],
                // renders regular text input
            ],
            'app.debug' => [
                'label' => __('Debug mode enabled'),
                'rules' => ['sometimes', 'required', 'boolean'],
                'cast' => 'boolean', // renders checkbox input
            ],
            'app.env' => [
                'label' => __('Application environment'),
                'rules' => ['sometimes', 'required'],
                'options' => [ // dont't mess `Item::$options` with drop-down options!
                    'options' => [ // renders drop-down input
                        ['value' => 'production', 'label' => 'Production'],
                        ['value' => 'local', 'label' => 'Local'],
                        ['value' => 'testing', 'label' => 'Testing'],
                    ],
                ],
            ],
            'app.description' => [
                'label' => __('Application description'),
                'rules' => ['sometimes', 'required'],
                'options' => [
                    'component' => 'textarea-field', // renders textarea input
                ],
            ],
            // ...
        ];
    }

    // ...
}

通常,任何允许用于资源管理表单(例如创建/更新记录表单)的Nova字段都可以用于特定的配置项。但是,某些字段需要额外的配置参数,您需要手动设置。为了获得适当的配置选项,您可以在您的某些Nova资源中设置您感兴趣的字段,然后转到该资源的创建表单,并在浏览器网络控制台中搜索类似于http://example.com/nova-api/your-resource-name/creation-fields的URL的XHR请求。其响应包含键为"fields"的JSON,其中您可以找到您字段的适当配置。

访问限制

您可以使用Nova提供的常规工具canSee()方法来限制对应用程序配置设置表单的访问。例如

<?php

namespace App\Providers;

use Laravel\Nova\NovaApplicationServiceProvider;

class NovaServiceProvider extends NovaApplicationServiceProvider
{
    public function tools()
    {
        return [
            (new \Illuminatech\NovaConfig\NovaConfig())
                ->canSee(function ($request) {
                    return $request->user(config('nova.guard'))->is_super_admin;
                }),
            // ...
        ];
    }
}

本地化

此扩展中使用的所有静态文本都可以通过Laravel本地化功能进行翻译。您可以使用以下命令发布翻译覆盖

php artisan vendor:publish --provider="Illuminatech\NovaConfig\NovaConfigServiceProvider" --tag=lang