|
| 1 | +# Session storage service |
| 2 | + |
| 3 | +This service helps when dealing with key-value arrays that must be saved indipendently inside the session. |
| 4 | + |
| 5 | +The classic use-case is when two or more CRUD-based entity lists have session-persisted filters: each list has its own key-value filters and they must be managed indipendently. |
| 6 | + |
| 7 | +## Symfony configuration |
| 8 | + |
| 9 | +If you want to use this service as a Symfony service, add the following configuration to your `services.yml` file: |
| 10 | + |
| 11 | +```yaml |
| 12 | +services: |
| 13 | + # Other services... |
| 14 | + gibilogic.session_storage: |
| 15 | + class: Gibilogic\Elements\Service\SessionStorageService |
| 16 | + arguments: |
| 17 | + - "@session" |
| 18 | +``` |
| 19 | +
|
| 20 | +You'll be able to get an instance of the service from the service container: |
| 21 | +
|
| 22 | +```php |
| 23 | +/* @var \Gibilogic\Elements\Service\SessionStorageService $sessionStorageService */ |
| 24 | +$sessionStorageService = $this->container->get('gibilogic.session_storage'); |
| 25 | +``` |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +With this service you can `set`, `get` and `remove` key-value arrays by giving them a name (aka. "key"). |
| 30 | + |
| 31 | +For setting a key-value array use the `set` method: |
| 32 | + |
| 33 | +```php |
| 34 | +$sessionStorageService->set('order', [ |
| 35 | + 'fromDate' => '2016-01-01', |
| 36 | + 'toDate' => '2016-02-01', |
| 37 | +]); |
| 38 | +``` |
| 39 | + |
| 40 | +Getting the data back is as simple as calling the `get` method: |
| 41 | + |
| 42 | +```php |
| 43 | +$filters = $sessionStorageService->get('order'); |
| 44 | +``` |
| 45 | + |
| 46 | +Call the `remove` method to clear your data: |
| 47 | + |
| 48 | +```php |
| 49 | +$sessionStorageService->remove('order'); |
| 50 | +``` |
| 51 | + |
| 52 | +### Setting with overwrite |
| 53 | + |
| 54 | +By default the `set` method will use an `array_replace` to save your data: existing data will be merged by replace with the new data. |
| 55 | + |
| 56 | +If you want to disable this feature, just pass a boolean `true` as second parameter to the `set` method: |
| 57 | + |
| 58 | +```php |
| 59 | +$sessionStorageService->set('order', [ |
| 60 | + 'fromDate' => '2016-01-01', |
| 61 | + 'toDate' => '2016-02-01', |
| 62 | +], true); |
| 63 | +``` |
| 64 | + |
| 65 | +The default behaviour can be useful when dealing with CRUD/APIs filters: you may want to update the changed values only, keeping the unchanged ones. |
0 commit comments