Skip to content

Laravel 5 persistent application-wide settings with Javascript helper function.

License

Notifications You must be signed in to change notification settings

CodeKoalas/laravel-settings

 
 

Repository files navigation

Laravel 5 Persistent Settings

Latest Version on Packagist Latest Version on Packagist Build Status StyleCI License

Store and manage application wide settings for Laravel 5. Supports multiple and custom storage drivers.

Installation

Install the package via composer using the following command

composer require mohd-isa/laravel-settings

Add the following service provider to config/app.php

Smartisan\Settings\SettingsServiceProvider::class,

Optionally, add the following facade to your config/app.php facades list

'Settings' => Smartisan\Settings\Facades\Settings::class,

Publish the configurations file to customize the package. This is optional for Laravel 5.5+

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

If you are going to use database driver, publish the table using the following command

php artisan settings:table

Features

  • Multiple storage drivers support
  • Caching
  • Support custom drivers
  • PHP and Javascript Helper Functions
  • Whitelist or Blacklist Settings

Usage

Set Method

Persist settings to the storage

// single value
Settings::set('key', 'value');

// multiple values
Settings::set('key', [
    'k1' => 'v1',
    'k2' => 'v2'
]);

// dot notation
Settings::set('key.k1', 'v1');

Get Method

Retrieves settings values from the storage. The result may be a single value or an array.

// simple key
Settings::get('key', 'default');

// dot notation
Settings::get('key.k1', 'default');

// all settings
Settings::get();

Forget Method

Removes given settings from the storage. If the key composites of multiple values, all values will be removed.

Settings::forget('key');

Has Method

Checks whether the given settings key exists or not.

Settings::has('key');

Flush Method

Removes all settings.

Settings::flush();

PHP Helper Function

Helper function is a handy function to set or get values from the setting storage. It accepts two parameters, key and default. Depends on the arguments number, set or get will be decided.

  • Get

The result may be a simple value or an array depends on the key value.

// simple key
settings('key', 'default');

// dot notation
settings('key.k1', 'default');
  • Set

You can set multiple settings at once. If the key already exists, the value will be updated.

settings([
   'k1' => 'v1',
   'k2' => [
      'k3' => 'v3',
      'k4' => 'v4'
   ]
])

Javascript Helper Function

In order to use javascript helper function, you only have to use @settings custom directive in your blade files. This will allow you to access the settings through settings(key, default) javascript helper function.

If you don't want to allow access to sensetive settings values, use blacklist or whitelist configuration options in settings.php

You can use dot notation to blacklist or whitelist multiple settings.

Custom Drivers

The package allows you to implement your own custom driver without the need of modifying its underlying code.

  • Step 1. Create Driver Class

Create the driver class in your app folder and make sure it implements Repository contract. The class name has the following naming convention: [Driver_Name]Driver.

use Arados\Settings\Contracts\Repository;

class DropboxDriver implements Repository
{
   ...
}
  • Step 2. Define Driver Config

If necessary, define your driver configuration in config/settings.php

return [
 ...
   'drivers' => [
      'dropbox' => ..
   ]
]
  • Step 3. Extend Settings Manager

In your service provider, you have to extend the settings manager to tell the package about the new driver

public function register()
{
   ...
   $this->app['settings.manager']->extends('dropbox', function($app) {
      return new DropboxDriver(..);
   });
}

Security

If you discover any security related issues, please email mohd.itcs@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

About

Laravel 5 persistent application-wide settings with Javascript helper function.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 97.8%
  • JavaScript 2.2%