Simple key/value typed settings for your Laravel app
Create, store and use
- key/value settings,
- typed from numbers over dates to array,
- cached for quick access and
- automatically synchronized to a configured disc as a static json export.
Create any setting you like
Setting::create([
'key' => 'setting.example.int',
'type' => 'integer',
'value' => 336,
]);
and get it back, anywhere in your app
$example = Setting::getValue('setting.example.int');
Installation
You can install the package via composer:
composer require elipzis/laravel-simple-setting
You can publish the config file with:
php artisan vendor:publish --tag="simple-setting-config"
This is the contents of the published config file:
return [
'repository' => [
//The table name where to store the settings.
'table' => 'settings',
//The used cache configuration
'cache' => [
'prefix' => 'settings',
'ttl' => 3600
]
],
'routing' => [
//Should routes be available to access the settings?
'enabled' => true,
//What path prefix to be used
'prefix' => 'setting',
//Any middleware?
'middleware' => [],
],
'sync' => [
//Where to statically sync the settings to
'disc' => env('FILESYSTEM_DRIVER', 'local'),
//The filename to write to
'filename' => 'settings.json',
//Whether to automatically (re-)sync the settings to the disc with every change
'auto' => true
]
];
Before you publish the migrations, publish the config, if you would like to alter e.g. the table name.
You can publish and run the migrations with:
php artisan vendor:publish --tag="simple-setting-migrations"
php artisan migrate
Usage
Creation
The following types can be used:
Setting::create([
'key' => 'setting.example.int',
'type' => 'integer',
'value' => 336,
]);
$now = Carbon::now();
Setting::create([
'key' => 'setting.example.datetime',
'type' => 'datetime', //or date
'value' => $now->addWeeks(2),
]);
Setting::create([
'key' => 'setting.example.bool',
'type' => 'boolean',
'value' => false
]);
Setting::create([
'key' => 'setting.example.array',
'type' => 'array',
'value' => [
'exampleA' => 'A',
'exampleB' => 'B',
'exampleC' => 'C',
]
]);
Setting::create([
'key' => 'setting.example.string',
'type' => 'string',
'value' => '((x^0.5)/0.9)+10'
]);
Retrieval
Return the whole model
Setting::get('test');
which would return something like
{"test":{"id":1,"key":"test","value":"test","type":"string","created_at":"2021-12-25T10:18:07.000000Z","updated_at":"2021-12-25T10:18:07.000000Z"}}
keyed by the key
.
If you just need the value, call
Setting::getValue('test');
which returns only the value, in this case test
.
Controller
If you have routing activated, you may access the settings via routes, e.g. GET https://yourdomain.tld/setting/{setting}
to get a setting by key.
Note: Routes only return values and have no setter
endpoint!
Command
Settings can/will be (re-)synced to your disc for static access automatically, if configured. You can (re-)sync these by calling the command
php artisan setting:sync
All settings will be exported to a json file to the configured disc.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.