outl1ne/nova-settings
Composer 安装命令:
composer require outl1ne/nova-settings
包简介
A Laravel Nova tool for editing custom settings using native Nova fields.
README 文档
README
This Laravel Nova package allows you to create custom settings in code (using Nova's native fields) and creates a UI for the users where the settings can be edited.
Requirements
php: >=8.0laravel/nova: ^4.26
Features
- Settings fields management in code
- UI for editing settings
- Helpers for accessing settings
- Rule validation support
Screenshots
Installation
Install the package in a Laravel Nova project via Composer and run migrations:
# Install nova-settings composer require outl1ne/nova-settings # Run migrations php artisan migrate
Register the tool with Nova in the tools() method of the NovaServiceProvider:
// in app/Providers/NovaServiceProvider.php public function tools() { return [ // ... new \Outl1ne\NovaSettings\NovaSettings ]; }
Usage
Registering fields
Define the fields in your NovaServiceProvider's boot() function by calling NovaSettings::addSettingsFields().
// Using an array \Outl1ne\NovaSettings\NovaSettings::addSettingsFields([ Text::make('Some setting', 'some_setting'), Number::make('A number', 'a_number'), ]); // OR // Using a callable \Outl1ne\NovaSettings\NovaSettings::addSettingsFields(function() { return [ Text::make('Some setting', 'some_setting'), Number::make('A number', 'a_number'), ]; });
Registering field panels
// Using an array \Outl1ne\NovaSettings\NovaSettings::addSettingsFields([ Panel::make('Panel Title', [ Text::make('Some setting', 'some_setting'), Number::make('A number', 'a_number'), ]), ]);
Casts
If you want the value of the setting to be formatted before it's returned, pass an array similar to Eloquent's $casts property as the second parameter.
\Outl1ne\NovaSettings\NovaSettings::addSettingsFields([ // ... fields ], [ 'some_boolean_value' => 'boolean', 'some_float' => 'float', 'some_collection' => 'collection', // ... ]);
Subpages
Add a settings page name as a third argument to list those settings in a custom subpage.
\Outl1ne\NovaSettings\NovaSettings::addSettingsFields([ Text::make('Some setting', 'some_setting'), Number::make('A number', 'a_number'), ], [], 'Subpage');
If you leave the custom name empty, the field(s) will be listed under "General".
To translate the page name, publish the translations and add a new key novaSettings.$subpage to the respective translations file, where $subpage is the name of the page (full lowercase, slugified).
Authorization
Show/hide all settings
If you want to hide the whole Settings area from the sidebar, you can authorize the NovaSettings tool like so:
public function tools(): array { return [ NovaSettings::make()->canSee(fn () => user()->isAdmin()), ]; }
Show/hide specific setting fields
If you want to hide only some settings, you can use ->canSee(fn () => ...) per field. Like so:
Text::make('A text field') ->canSee(fn () => user()->isAdmin()),
Helper functions
nova_get_settings($keys = null, $defaults = [])
Call nova_get_settings() to get all the settings formated as a regular array. Additionally, you can pass a key => value array as a second argument: nova_get_settings(['some_key], ['some_key' => 'default_value']).
nova_get_setting($key, $default = null)
To get a single setting's value, call nova_get_setting('some_setting_key'). It will return either a value or null if there's no setting with such key.
You can also pass default value as a second argument nova_get_setting('some_setting_key', 'default_value'), which will be returned, if no setting was found with given key.
nova_set_setting_value($key, $value = null)
Sets a setting value for the given key.
Configuration
The config file can be published using the following command:
php artisan vendor:publish --provider="Outl1ne\NovaSettings\NovaSettingsServiceProvider" --tag="config"
| Name | Type | Default | Description |
|---|---|---|---|
base_path |
String | nova-settings |
URL path of settings page. |
reload_page_on_save |
Boolean | false | Reload the entire page on save. Useful when updating any Nova UI related settings. |
models.settings |
Model | Settings::class |
Optionally override the Settings model. |
cache |
String | :memory: |
Cache store name to use that cache, ":memory:" for singleton class, or null to turn off caching. |
The migration can also be published and overwritten using:
php artisan vendor:publish --provider="Outl1ne\NovaSettings\NovaSettingsServiceProvider" --tag="migrations"
Localization
The translation file(s) can be published by using the following command:
php artisan vendor:publish --provider="Outl1ne\NovaSettings\NovaSettingsServiceProvider" --tag="translations"
You can add your translations to resources/lang/vendor/nova-settings/ by creating a new translations file with the locale name (ie et.json) and copying the JSON from the existing en.json.
Credits
License
Nova Settings is open-sourced software licensed under the MIT license.
outl1ne/nova-settings 适用场景与选型建议
outl1ne/nova-settings 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.05M 次下载、GitHub Stars 达 297, 最近一次更新时间为 2022 年 05 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「nova」 「laravel」 「Settings」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 outl1ne/nova-settings 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 outl1ne/nova-settings 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 outl1ne/nova-settings 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel Nova card that shows you your system information.
Persistent settings package for Laravel framework.
A Laravel Nova card.
A Zend Framework module to quickly and easily set PHP settings.
A Laravel Nova package for publishable fields
A Laravel Nova package for translatable fields
统计信息
- 总下载量: 1.05M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 297
- 点击次数: 17
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-05-13
