abdelhamiderrahmouni/laravel-settings
Composer 安装命令:
composer require abdelhamiderrahmouni/laravel-settings
包简介
Production ready, super simple settings management for your laravel app.
README 文档
README
Production-ready, super-simple settings management for Laravel applications.
Store settings in the database with full support for per-user scoping, typed values, caching, and a clean enum-driven API.
Requirements
- PHP 8.2+
- Laravel 11, 12, or 13
Installation
composer require abdelhamiderrahmouni/laravel-settings
Publish and run the migration:
php artisan vendor:publish --tag=settings-migrations php artisan migrate
Configuration
Publish the config file to customise the cache driver, TTL, cache key prefix, and table name:
php artisan vendor:publish --tag=settings-config
// config/settings.php return [ 'cache' => [ 'driver' => env('SETTINGS_CACHE_DRIVER', 'file'), 'ttl' => env('SETTINGS_CACHE_TTL', 3600), 'prefix' => env('SETTINGS_CACHE_PREFIX', 'settings'), ], 'table' => env('SETTINGS_TABLE', 'settings'), ];
Defining Settings
Create a settings enum with the settings:make command:
php artisan settings:make GeneralSettings
This generates app/Settings/GeneralSettings.php. Fill in your cases:
namespace App\Settings; use Settings\Contracts\SettingDefinition; enum GeneralSettings: string implements SettingDefinition { case SiteName = 'site_name'; case MaintenanceMode = 'maintenance_mode'; case MaxUploadSize = 'max_upload_size'; public function group(): string { return 'general'; } public function type(): string { return match ($this) { self::SiteName => 'string', self::MaintenanceMode => 'boolean', self::MaxUploadSize => 'integer', }; } public function default(): mixed { return match ($this) { self::SiteName => 'My App', self::MaintenanceMode => false, self::MaxUploadSize => 10, }; } public function label(): string { return match ($this) { self::SiteName => 'Site Name', self::MaintenanceMode => 'Maintenance Mode', self::MaxUploadSize => 'Max Upload Size (MB)', }; } }
Supported types (Eloquent cast strings): string, integer, boolean, float, array, json.
Usage
Reading settings
use App\Settings\GeneralSettings; use Settings\Facades\Settings; // Returns the stored value, cast to the declared type Settings::get(GeneralSettings::SiteName); // Returns 'Fallback' if no record exists in the DB Settings::get(GeneralSettings::SiteName, 'Fallback');
Reading all settings in a group
Pass the enum class-string to retrieve every setting in the group as a single array. One DB query is issued and all values are cast and cached individually.
$settings = Settings::get(GeneralSettings::class); // [ // 'site_name' => 'My App', // 'maintenance_mode' => false, // 'max_upload_size' => 10, // ]
Any key without a DB record falls back to the enum's declared default.
Writing settings
Settings::set(GeneralSettings::SiteName, 'My App'); Settings::set(GeneralSettings::MaintenanceMode, true); Settings::set(GeneralSettings::MaxUploadSize, 25);
Writing multiple settings at once
Pass the enum class-string and an array of case value => value pairs. Unknown keys are silently ignored; each recognised key is validated against the enum.
Settings::set(GeneralSettings::class, [ 'site_name' => 'My App', 'maintenance_mode' => true, 'max_upload_size' => 25, ]);
Deleting settings
// Removes the DB row. Subsequent get() calls return the enum default. Settings::forget(GeneralSettings::SiteName);
Per-user settings
Scope any operation to a specific user with for(). This includes bulk get and set:
Settings::for($user)->get(GeneralSettings::SiteName); Settings::for($user)->set(GeneralSettings::SiteName, 'Their App'); Settings::for($user)->forget(GeneralSettings::SiteName); // Bulk operations are scoped too Settings::for($user)->get(GeneralSettings::class); Settings::for($user)->set(GeneralSettings::class, ['site_name' => 'Their App']);
Global settings and per-user settings are stored and retrieved independently.
Dependency injection
The SettingsManager is bound as a singleton in the container and can be injected directly:
use Settings\SettingsManager; class UpdateSiteNameAction { public function __construct(private readonly SettingsManager $settings) {} public function handle(string $name): void { $this->settings->set(GeneralSettings::SiteName, $name); } }
Caching
Settings are cached per-key after the first read. The cache is automatically invalidated when set() or forget() is called — no manual cache management needed.
Cache keys follow the format: {prefix}:{group}.{name} (or {prefix}:{group}.{name}:{user_id} for per-user settings).
Customising the stub
To customise the enum template generated by settings:make, publish the stub:
php artisan vendor:publish --tag=settings-stubs
This copies stubs/settings.stub into your project. The settings:make command will use your published stub automatically.
Testing
The package test suite uses Pest:
composer test
License
MIT — see LICENSE for details.
abdelhamiderrahmouni/laravel-settings 适用场景与选型建议
abdelhamiderrahmouni/laravel-settings 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 04 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「configuration」 「laravel」 「Settings」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 abdelhamiderrahmouni/laravel-settings 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 abdelhamiderrahmouni/laravel-settings 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 abdelhamiderrahmouni/laravel-settings 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Persistent settings package for Laravel framework.
Configuration component
A Zend Framework module to quickly and easily set PHP settings.
Settings Card for Laravel Nova.
Utils to load, parse and work with configuration on Mezzio projects
Extends basic Wordpress features.
统计信息
- 总下载量: 9
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 32
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-04-27