@@ -384,20 +384,35 @@ By default all resource objects will be encoded with their `self` link, e.g.:
384
384
}
385
385
```
386
386
387
- You can change this behaviour by overloading the ` getResourceLinks ` or ` getIncludedResourceLinks ` methods.
388
- For example :
387
+ You can change this behaviour by implementing the ` getResourceLinks ` method. For example, if you do not want any links
388
+ to be serialized :
389
389
390
390
``` php
391
391
class Schema extends SchemaProvider
392
392
{
393
393
// ...
394
394
395
- public function getResourceLinks($resource)
395
+ public function getResourceLinks($resource): ?array
396
396
{
397
- $links = parent::getResourceLinks($resource);
398
- $links['foo'] = $this->createLink('posts/foo');
397
+ return null;
398
+ }
399
+ }
400
+ ```
401
+
402
+ If you return an array without any ` self ` key in it, the ` self ` link will be automatically added. If you do not want
403
+ the ` self ` link to be set, set the array key ` self ` to ` false ` .
404
+
405
+ ``` php
406
+ class Schema extends SchemaProvider
407
+ {
408
+ // ...
399
409
400
- return $links;
410
+ public function getResourceLinks($resource): array
411
+ {
412
+ return [
413
+ // "self" will automatically be added as it is not set to false.
414
+ 'foo' => $this->createLink('posts/foo'),
415
+ ];
401
416
}
402
417
403
418
}
@@ -423,33 +438,23 @@ This would result in the following resource object:
423
438
> The ` createLink ` method allows you to pass in link meta and set whether the URI is relative to the API or an
424
439
absolute path.
425
440
426
- If you want to only change the links when the resource is appearing in the ` included ` section of the JSON API
427
- document, overload the ` getIncludedResourceLinks() ` method instead.
428
-
429
441
## Meta
430
442
431
- You can add top-level ` meta ` to your resource object using the ` getPrimaryMeta() ` or ` getInclusionMeta() ` methods
432
- on your schema. These are called depending on whether your resource is appearing in either the primary ` data `
433
- member of the JSON API document or the ` included ` member.
443
+ You can add top-level ` meta ` to your resource object using the ` getResourceMeta() ` method
444
+ on your schema.
434
445
435
- For example, the following would add meta to your resource object regardless of whether it is primary data or
436
- included in the document:
446
+ For example:
437
447
438
448
``` php
439
449
class Schema extends SchemaProvider
440
450
{
441
451
// ...
442
452
443
- public function getPrimaryMeta ($resource)
453
+ public function getResourceMeta ($resource)
444
454
{
445
455
return ['foo' => 'bar'];
446
456
}
447
457
448
- public function getInclusionMeta($resource)
449
- {
450
- return $this->getPrimaryMeta($resource);
451
- }
452
-
453
458
}
454
459
```
455
460
0 commit comments