Skip to content

⚙️ Simple key/value typed settings for your Laravel app with synchronized json export

License

Notifications You must be signed in to change notification settings

elipZis/laravel-simple-setting

Repository files navigation

Simple key/value typed settings for your Laravel app

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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');

or access the statically created e.g. settings.json export to reduce Webserver load!

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.

Static export

Every change/creation of a setting is automatically updating a statically exported file, by default settings.json on your default filesystem disc. This should ensure a reduced Webserver load for external access by e.g. your SPA frontend so that they just need to access a for example to S3 exported CDN-cached file, without "hammering" the Webserver every time.

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.

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!

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.

About

⚙️ Simple key/value typed settings for your Laravel app with synchronized json export

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Sponsor this project

Languages