5
5
Laravel provides a number of global [ helper PHP functions] ( https://laravel.com/docs/helpers ) . This package
6
6
provides some additional helpers that allow you to manually use capabilities within the package.
7
7
8
+ Helpers are either * global* (like Laravel's helpers) or scoped to a specific API. The latter we refer to as
9
+ * API helpers* .
10
+
8
11
## Global Helpers
9
12
10
13
### ` json_api() `
@@ -35,6 +38,16 @@ This will return the JSON API request instance, or `null` if there is no inbound
35
38
36
39
The following helpers methods are available on the API instances.
37
40
41
+ ### ` client() `
42
+
43
+ This helper returns a HTTP client for [ sending JSON API requests] ( ./http-clients/ ) to remote servers. You
44
+ must provide a Guzzle client as the first argument:
45
+
46
+ ``` php
47
+ $guzzleClient = new GuzzleHttp\Client(['base_uri' => 'http://example.com/api/']);
48
+ $client = json_api('v1')->client($guzzleClient);
49
+ ```
50
+
38
51
### ` encoder() `
39
52
40
53
Gets an encoder that is configured with the API's details. This can be used to manually serialize data to arrays, or
@@ -55,12 +68,59 @@ depth arguments in PHP's native `json_encode()` method.
55
68
echo json_api('v1')->encoder(JSON_PRETTY_PRINT)->encodeData($post);
56
69
```
57
70
58
- ### ` client ()`
71
+ ### ` links ()`
59
72
60
- This helper returns a HTTP client for [ sending JSON API requests] ( ./http-clients/ ) to remote servers. You
61
- must provide a Guzzle client as the first argument:
73
+ This helper returns a generator that allows you to create JSON API link objects for URLs in your API. For example:
62
74
63
75
``` php
64
- $guzzleClient = new GuzzleHttp\Client(['base_uri' => 'http://example.com/api/']);
65
- $client = json_api('v1')->client($guzzleClient);
76
+ /** @var \Neomerx\JsonApi\Contracts\Document\LinkInterface $link */
77
+ $link = json_api('v1')->links()->index('posts');
78
+ $meta = ['foo' => 'bar'];
79
+ $link = json_api('v1)->links()->index('posts', $meta);
66
80
```
81
+
82
+ The following methods on the links generator create links for resources within your API:
83
+
84
+ - ` index($type, $meta) `
85
+ - ` create($type, $meta) `
86
+ - ` read($type, $id, $meta) `
87
+ - ` update($type, $id, $meta) `
88
+ - ` delete($type, $id, $meta) `
89
+ - ` relatedResource($type, $id, $relationship, $meta) `
90
+ - ` readRelationship($type, $id, $relationship, $meta) `
91
+ - ` replaceRelationship($type, $id, $relationship, $meta) `
92
+ - ` addRelationship($type, $id, $relationship, $meta) `
93
+ - ` removeRelationship($type, $id, $relationship, $meta) `
94
+
95
+ > In all of these methods, ` $meta ` is an optional argument.
96
+
97
+ All these methods take an additional optional argument - an array of parameters. Use this if you need to pass
98
+ additional parameters that are required when generating the URLs within the link objects.
99
+
100
+ ### ` url() `
101
+
102
+ This helper returns a URL generator for the API, which makes it easy to generate string links to resources within
103
+ the API. For example:
104
+
105
+ ``` php
106
+ // http://localhost/api/v1/posts
107
+ $url = json_api('v1')->url()->index('posts');
108
+ // http://localhost/api/posts/1
109
+ $url = json_api('v1')->url()->read('posts', 1);
110
+ ```
111
+
112
+ The available methods on the URL generator are as follows:
113
+
114
+ - ` index($type) `
115
+ - ` create($type) `
116
+ - ` read($type, $id) `
117
+ - ` update($type, $id) `
118
+ - ` delete($type, $id) `
119
+ - ` relatedResource($type, $id, $relationship) `
120
+ - ` readRelationship($type, $id, $relationship) `
121
+ - ` replaceRelationship($type, $id, $relationship) `
122
+ - ` addRelationship($type, $id, $relationship) `
123
+ - ` removeRelationship($type, $id, $relationship) `
124
+
125
+ All these methods take an additional optional argument - an array of parameters. Use this if you need to pass
126
+ additional parameters that are required when generating the URLs.
0 commit comments