8000 [Docs] Improve media-types chapter · CodingSeo/laravel-json-api@6c84659 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6c84659

Browse files
committed
[Docs] Improve media-types chapter
Closes cloudcreativity#349
1 parent 72f8000 commit 6c84659

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

docs/features/media-types.md

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,8 @@ the request as its first argument. For a create request, the argument will be `n
504504
resource. E.g. `GET /api/v1/posts` or when the resource is in a relationship such as
505505
`GET /api/v1/users/1/posts`.
506506

507+
#### Encoding Example
508+
507509
For example, say we wanted to support returning an avatar's image via our API we would need to
508510
support the media type of the stored avatar. Our avatar content negotiator may look like this:
509511

@@ -534,7 +536,7 @@ class ContentNegotiator extends BaseContentNegotiator
534536
```
535537

536538
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
538540
request method is `GET`.
539541

540542
> The `EncodingList` class also has an `unless` method, along with other helper methods.
@@ -594,6 +596,9 @@ class ContentNegotiator extends BaseContentNegotiator
594596
}
595597
```
596598

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+
597602
The media types listed on your content negotiator are **added** to the list of media types that
598603
your API supports. They will be used for every controller action that the content negotiator
599604
is used for.
@@ -609,12 +614,35 @@ be `null`.
609614
relationship object. E.g. `POST /api/v1/posts/1/tags`. This method receives the domain record for
610615
the request as its first arguments, and the relationship field name as its second argument.
611616

617+
#### Decoding Example
618+
612619
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:
614621

615622
```php
623+
namespace App\JsonApi;
616624

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;
618646

619647
use App\Avatar;
620648
use App\JsonApi\MultipartDecoder;

0 commit comments

Comments
 (0)
0