Download the PHP package bonsaicms/settings without Composer
On this page you can find all versions of the php package bonsaicms/settings. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download bonsaicms/settings
More information about bonsaicms/settings
Files in bonsaicms/settings
Informations about the package settings
Introduction
There are some "settings packages" for Laravel out there. For example anlutro/laravel-settings or akaunting/setting but we think we can do better.
For example, this package is able to save any value in the settings (numbers, strings, booleans etc. but also any objects, for example Eloquent models).
How it works
- This package serialize the setting value (using the PHP's
serialize
andbase64_encode
functions). - The value is stored it in a
text
database column type (if you useDatabaseSettingsRepository
). - When reading the setting value, the serialized value is decoded (via PHP
base64_decode
function) and then deserialized (via PHPdeserialize
function).
Installation
Publish config file
Run migrations
Auto-load Middleware
Add the following line inside your app/Http/Kernel.php
file. This middleware will automatically call Settings::all()
before each request so whenever you call Settings::get()
there will be no DB query executed, because all settings will be already in the cache.
Auto-save Middleware
Add the following line inside your app/Http/Kernel.php
file. This middleware will automatically call Settings::save()
after each request so you won't need to manually call it.
Usage
Set / Get Eloquent Models
To make this work, your models need to implement our BonsaiCms\Settings\Contracts\SerializationWrappable
interface. You can implement serialization logic by yourself, but you can also use our BonsaiCms\Settings\Models\SerializableModelTrait
trait.
Our BonsaiCms\Settings\Models\SerializableModelTrait
will NOT serialize the model's attributes! It will only serialize the model identity (model name + ID) and it will retrieve that model from the database when you call Settings::get(...)
.
If you need different serialization behaviour you need to implement your own serialization logic by implementing our BonsaiCms\Settings\Contracts\SerializationWrappable
interface.
Example of your model class
Has
Save
Delete
Artisan command to delete settings
This will call Settings::deleteAll()
under the hood.
Facade vs Helper
There is also a settings()
helper available.
Save your own objects in settings
Any of your classes can implement our BonsaiCms\Settings\Contracts\SerializationWrappable
interface. It should then implement these two methods:
Example implementation:
Then you can simply write:
Settings and JSON-like API
Do you need to work with settings via API ? Check out our bonsaicms/settings-api package.