Home

Awesome

Package is looking for maintainers Please contact me if interested.

Persistent Settings for Laravel 5

Build Status


Persistent settings package for Laravel 5.



Installation

Require this package in your composer.json:

"require": {
  "edvinaskrucas/settings": "2.0.0"
}

Version matrix

Laravel VersionPackage version
>=5.0, <=5.1>= 1.0.0, <= 2.0.0
5.2>= 2.0.0

Registering to use it with laravel

Add following lines to app/config/app.php

ServiceProvider array

Krucas\Settings\Providers\SettingsServiceProvider::class,

Alias array

'Settings' => Krucas\Settings\Facades\Settings::class

Publishing config file

If you want to edit default config file, just publish it to your app folder.

php artisan vendor:publish --provider="Krucas\Settings\Providers\SettingsServiceProvider" --tag="config"

Usage

Configuration

Package comes with several configuration options.

SettingDescription
defaultSetting repository driver.
cacheEnable or disable setting cache.
encryptionEnable or disable setting value encryption.
eventsEnable or disable event firing.
repositoriesConfig of all repositories which can be used.
key_generatorKey generator class.
context_serializerContext serializer class.
value_serializerValue serializer class.
overrideAllows you to override values in Laravel config array.

Creating table for database driver

To use database driver you have to create table in your database. Package provides default table migration, to create it you need to execute artisan command:

$ php artisan settings:table

Methods

Set value

Set setting value.

Settings::set($key, $value = null);

Get value

Get setting value, default value is returned when no value found.

Settings::get($key, $default = null);

Check value

Determine if setting exists.

Settings::has($key);

Forget value

Forget setting value from repository.

Settings::forget($key);

Set context

Setting values may be used in certain context. Context can be set using method context().

Settings::context(new Context(['user' => 1]));

Context is reset after call of one these methods set, get, has, forget. Example how to use settings for different contexts.

$userContext1 = new Context(['user' => 1]);
$userContext2 = new Context(['user' => 2]);
Settings::context($userContext1)->set('key', 'value1');
Settings::context($userContext2)->set('key', 'value2');

// retrieve settings
$userValue1 = Settings::context($userContext1)->get('key'); // value1
$userValue2 = Settings::context($userContext2)->get('key'); // value2

Helpers

Settings service instance

Resolve settings service instance.

settings();

Set value

Set setting value.

settings([$key => $value]);

Set setting value for a context.

settings([$key => $value], new Context(['user' => 1]));

Get value

Get setting value, default value is returned when no value found.

settings($key, $default = null);

Getting value for a context.

settings($key, $default, new Context(['user' => 1]));

Events

Events gets fired if this is not disabled via config (enabled by default).

settings.checking: $key

Fired before checking if value is present in repository.

ParameterTypeParameter description
$keystringSetting key.
$contextnull or ContextSetting context.

settings.has: $key

Fired after checking if value is present in repository.

ParameterTypeParameter description
$keystringSetting key.
$statusboolIf setting exists true is passed, otherwise false
$contextnull or ContextSetting context.

settings.getting: $key

Fired before retrieving value from repository.

ParameterTypeParameter description
$keystringSetting key.
$defaultmixedDefault setting value.
$contextnull or ContextSetting context.

settings.get: $key

Fired after retrieving value from repository.

ParameterTypeParameter description
$keystringSetting key.
$valuemixedRetrieved setting value.
$defaultmixedDefault setting value.
$contextnull or ContextSetting context.

settings.setting: $key

Fired before setting value to repository.

ParameterTypeParameter description
$keystringSetting key.
$valuemixedSetting value to be set.
$contextnull or ContextSetting context.

settings.set: $key

Fired after setting value to repository.

ParameterTypeParameter description
$keystringSetting key.
$valuemixedSetting value to be set.
$contextnull or ContextSetting context.

settings.forgetting: $key

Fired before forgetting value.

ParameterTypeParameter description
$keystringSetting key.
$contextnull or ContextSetting context.

settings.forget: $key

Fired after forgetting value.

ParameterTypeParameter description
$keystringSetting key.
$contextnull or ContextSetting context.