tahiriabdou/laravel-settings

Downloads

30

Stars

1

Version

1.0.3

Laravel Settings

Build Status

Laravel settings it's a composer package made for our projects that allows developers to persist settings into database or json files.

Installation

PHP 5.4+ and Laravel 5.* are required.

To get the latest version of Laravel Settings run the following command:

composer require ionutmilica/laravel-settings

Once Laravel Settings is installed, you need to register the service provider. Open up config/app.php and add the following to the providers key.

  • 'IonutMilica\LaravelSettings\SettingsServiceProvider'

You can register the Settings facade in the aliases key of your config/app.php file if you like.

  • 'Settings' => 'IonutMilica\LaravelSettings\Facade'

If you want to have persistent settings, you will need to add a new middleware in app/Http/Kernel.php to the middleware key.

  • 'IonutMilica\LaravelSettings\SavableMiddleware',

Laravel settings default driver is set to json. If you want to change it you can execute artisan vendor:publish command and then modify app/config/settings.php file.

If you chose database driver you should also migrate the database with php artisan migrate.

How to use it

For simple usage we provide a helper that provides all the features you need:

  • Fetching a value
$canRegister = settings('restrictions.register');

if (! $canRegister) {
	// do something
}

// Fetching with default value

$canRegister = settings('restrictions.register', false);

// Fetching and save the setting if it does not exist

$canRegister = settings('restrictions.register, false, true);

  • Storing a new setting or editing an old one?
settings()->set('my-setting', 'some-value');
  • Checking for a setting existence
if (settings()->has('my-setting')) {
    // do something
}
  • Wanting to delete a not needed setting?
settings()->forgot('my-setting');
  • Force the saving of the settings into the datastore
settings()->set('the-answer', 42);
settings()->save();

You can also inject the settings instance into your laravel controller:


use IonutMilica\LaravelSettings\SettingsContract;

class RegistrationController extends Controller {

    public function register(Request $request, SettingsContract $settings)
    {
        if ($settings->get('restrictions.registration')) {
        	return redirect()->back();
        }
        
        // Do something
    }
}

More examples will follow!

TahiriAbdou

Author

TahiriAbdou