gocrew / laravel-settings
Persistant settings in Laravel.
Requires
- php: >=5.5.0
- illuminate/http: ~5
- illuminate/support: ~5
Requires (Dev)
- illuminate/database: ~5
- illuminate/filesystem: ~5
- mockery/mockery: 0.9.*
Suggests
- illuminate/database: Save settings to a database table.
- illuminate/filesystem: Save settings to a JSON file.
This package is not auto-updated.
Last update: 2017-11-19 20:17:10 UTC
README
Installation
Add Presenter to your composer.json file:
"require": { "gocrew/laravel-settings": "~1.0" }
Now, run a composer update on the command line from the root of your project:
composer update
Registering the Package
Include the service provider within app/config/app.php
. The service povider is needed for the generator artisan command.
'providers' => [ ... gocrew\LaravelSettings\SettingsServiceProvider::class ... ];
Now publish the config file and migrations by running php artisan vendor:publish
. The config file will give you control over which storage engine to use as well as some storage-specific settings.
Optionally you can also register the Facade of this package.
'facades' => [ ... 'Setting' => gocrew\LaravelSettings\Facade::class ... ];
IMPORTANT if you are using the database driver don't forget to migrate the database by running php artisan migrate
Usage
You can either access the setting store via its facade or inject it by type-hinting the contact gocrew\LaravelSettings\Contracts\SettingsContract
.
Setting::set('foo', 'bar'); $setting = Setting::get('foo', 'default value');
Call Setting::save()
explicitly to save changes made.
Auto-saving
If you add the middleware gocrew\LaravelSettings\Middleware\SavableMiddleware.php
to your middleware
list in app\Http\Kernel.php
, settings will be saved automatically at the end of all HTTP requests.
You still need to call Setting::save()
explicitly in console commands, queue workers etc.
Database storage
If you want to store settings for multiple users/clients in the same database you can do so by specifying extra columns:
Setting::setExtraColumns(array( 'user_id' => Auth::user()->id ));
where user_id = x
will now be added to the database query when settings are retrieved, and when new settings are saved, the user_id
will be populated.
If you need more fine-tuned control over which data gets queried, you can use the setConstraint
method which takes a closure with two arguments:
$query
is the query builder instance$insert
is a boolean telling you whether the query is an insert or not. If it is an insert, you usually don't need to do anything to$query
.
Setting::setConstraint(function($query, $insert) { if ($insert) return; $query->where(/* ... */); });
Custom drivers
This package uses the Laravel Manager
class under the hood, so it's easy to add your own custom settings store driver.
All you need to do is extend the abstract gocrew\LaravelSettings\Drivers\Driver
class, implement the abstract methods and call Setting::extend
.
class MyDriver extends gocrew\LaravelSettings\Drivers\Driver { /** * Read the data from the store. * * @return array */ protected function read() { // TODO: Implement read() method. } /** * Write the data into the store. * * @param array $data * * @return void */ protected function write(array $data) { // TODO: Implement write() method. } } Setting::extend('mydriver', function($app) { return $app->make('MyDriver'); });
License
The contents of this repository is released under the MIT license.