d3lph1/laravel-configger

This package is abandoned and no longer maintained. No replacement package was suggested.

Persistent settings for your Laravel app

1.1 2017-08-16 13:46 UTC

This package is not auto-updated.

Last update: 2018-07-22 18:04:28 UTC


README

Persistent settings for your Laravel app.

There is the possibility of storing data in the database, in Redis and also in a file on disk.

Requirements:

  • Laravel 5.3+

If you want to use the Redis driver then you need to install predis/predis package.

Installation

Execute command:

composer require d3lph1/laravel-configger ~1.1

or add line to require section of composer.json

"d3lph1/laravel-configger": "~1.1"

and execute:

composer update

Add item in array providers in your config/app.php:

D3lph1\LaravelConfigger\ServiceProvider::class,

And, if you wish, the alias in the array aliases in the same file:

'Configger' => D3lph1\LaravelConfigger\Facade::class,

Execute:

php artisan vendor:publish --provider=D3lph1\LaravelConfigger\ServiceProvider

Configure the created config/configger.php file.

If you plan to use a database as a repository, you need to migrate with the php artisan migrate.

Usage

Set new setting value:

Configger::set('option.name', 'option_value');

Get the setting value:

$result = Configger::get('option.name');

Check setting for existence:

if (Configger::has('option.name')) {
    // do something...
} else {
    // do something...
}

Get the value of the setting and if it does not exist, return the default value:

$result = Configger::get('option.name', 'default value');

Get all settings as array option => value:

$all = Configger::all();

Remove setting:

Configger::forget('option.name);

This method returns the value of the setting before deleting it.

Remove all settings:

Configger::flush();

Change current driver:

Configger::driver('redis');

Blade directives

Get the setting value:

@cfg('option.name')

Get the value of the setting and if it does not exist, return the default value:

@cfg('option.name', 'default value')

Check for existing settings:

@hasCfg('option.name')
    {{-- It will be executed if the setting exists --}}
@elseHasCfg
    {{-- It will be executed if the setting does not exists --}}
@endHasCfg

You can omit the else block if you do not need it.

If the value of the setting after casting is true:

@ifCfg('option.name')
    {{-- true --}}
@elseIfCfg
    {{-- false --}}
@endIfCfg

This is equivalent to the code:

@if(\Configger::get('option.name'))
    {{-- true --}}
@else
    {{-- false --}}
@endif

You can omit the else block if you do not need it.

Console commands

Export settings

Okay, imagine that you filled out some storage with settings, but suddenly you needed to change the repository. What to do? Do you manually fill the new repository with the same settings? No, just use the command:

php artisan configger:export {from} {to}

Where {from} is the name of the storage (driver) from which you want to export the settings, {to} - the repository to which you want to copy the settings. For example: php artisan configger:export database json export settings from database into JSON-file.

You can use the --clear option to clear the storage from which the import was made.

Basic

To your satisfaction, Laravel Configger can work with settings using console commands.

All commands further work with the driver specified in the configuration file config / configger.php, in order to interact with another repository use the option --driver. For example --driver=redis.

Set setting value:

php artisan configger:set option.name option_value

Get setting value:

php artisan configger:get option.name

Get all settings:

php artisan configger:all

Check setting for existence:

php artisan configger:has option.name

Remove setting:

php artisan configger:remove option.name

Remove all settings:

php artisan configger:flush