@@ -504,6 +504,8 @@ the request as its first argument. For a create request, the argument will be `n
504
504
resource. E.g. ` GET /api/v1/posts ` or when the resource is in a relationship such as
505
505
` GET /api/v1/users/1/posts ` .
506
506
507
+ #### Encoding Example
508
+
507
509
For example, say we wanted to support returning an avatar's image via our API we would need to
508
510
support the media type of the stored avatar. Our avatar content negotiator may look like this:
509
511
@@ -534,7 +536,7 @@ class ContentNegotiator extends BaseContentNegotiator
534
536
```
535
537
536
538
In this example, ` encodingMediaTypes() ` returns the list of the encodings supported by our API. The
537
- ` when ` method adds an encoding to the list if the first argument is true - in this case, if the
539
+ ` when ` method adds an encoding to the list if the first argument is ` true ` - in this case, if the
538
540
request method is ` GET ` .
539
541
540
542
> The ` EncodingList ` class also has an ` unless ` method, along with other helper methods.
10000
div>
@@ -594,6 +596,9 @@ class ContentNegotiator extends BaseContentNegotiator
594
596
}
595
597
```
596
598
599
+ > The ` MultipartDecoder ` is a decoder you will need to write with your own logic. There's an
600
+ example of what a decoder might look like below.
601
+
597
602
The media types listed on your content negotiator are ** added** to the list of media types that
598
603
your API supports. They will be used for every controller action that the content negotiator
599
604
is used for.
@@ -609,12 +614,35 @@ be `null`.
609
614
relationship object. E.g. ` POST /api/v1/posts/1/tags ` . This method receives the domain record for
610
615
the request as its first arguments, and the relationship field name as its second argument.
611
616
617
+ #### Decoding Example
618
+
612
619
For example, if we wanted to support uploading an Avatar image to create an ` avatars ` resource,
613
- our content negotiator could be :
620
+ we would need to write a decoder that handled files :
614
621
615
622
``` php
623
+ namespace App\JsonApi;
616
624
617
- namespace DummyApp\JsonApi\Avatars;
625
+ use CloudCreativity\LaravelJsonApi\Contracts\Decoder\DecoderInterface;
626
+
627
+ class MultipartDecoder implements DecoderInterface
628
+ {
629
+
630
+ /**
631
+ * @inheritdoc
632
+ */
633
+ public function decode($request): array
634
+ {
635
+ // return whatever array data you expect from the request.
636
+ // in this example we are expecting a file, so we will return all files.
637
+ return $request->allFiles();
638
+ }
639
+ }
640
+ ```
641
+
642
+ Then we would need to use this decoder in our ` avatars ` content negotiator:
643
+
644
+ ``` php
645
+ namespace App\JsonApi\Avatars;
618
646
619
647
use App\Avatar;
620
648
use App\JsonApi\MultipartDecoder;
0 commit comments