Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/liyu001989/dingo-serializer-switch
A middleware to switch fractal serializers in dingo
https://github.com/liyu001989/dingo-serializer-switch
dingo laravel serializer transformer
Last synced: 2 months ago
JSON representation
A middleware to switch fractal serializers in dingo
- Host: GitHub
- URL: https://github.com/liyu001989/dingo-serializer-switch
- Owner: liyu001989
- License: mit
- Created: 2017-06-28T01:33:53.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2022-11-23T02:42:25.000Z (about 2 years ago)
- Last Synced: 2024-10-05T15:46:25.023Z (3 months ago)
- Topics: dingo, laravel, serializer, transformer
- Language: PHP
- Size: 13.7 KB
- Stars: 48
- Watchers: 2
- Forks: 12
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Dingo Serializer Switch
This is a middleware for dingo/api to switch serializer
If a resource's relationship is null, we can return `$this->null()` in a transformer. But default serializer return [], I think null is better.
Also when pagination has no links, default is [], I wish to return null. So I write two serializer to fix this.If you want to use default serializer just use `default_array` or `default_data_array`.
## Installation
### Laravel
- `composer require liyu/dingo-serializer-switch`
### Lumen
*bootstrap/app.php*
```php
$app->routeMiddleware([
// ...
'serializer' => Liyu\Dingo\SerializerSwitch::class,
]);
```## Usage
```
$api->version('v1',
['middleware' => 'serializer:array'],
function ($api) {
});$api->version('v2',
['middleware' => 'serializer'],
function ($api) {
});$api->version('v3',
['middleware' => 'serializer:data_array'],
function ($api) {
});
```default key is data_array, so v2 v3 is same.
```
'default_array' => 'League\Fractal\Serializer\ArraySerializer',
'default_data_array' => 'League\Fractal\Serializer\DataArraySerializer',
'json_api' => 'League\Fractal\Serializer\JsonApiSerializer','array' => 'Liyu\Dingo\Serializers\ArraySerializer',
'data_array' => 'Liyu\Dingo\Serializers\DataArraySerializer',
```## License
[MIT LICENSE](https://github.com/liyu001989/dingo-serializer-switch/blob/master/LICENSE)