You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/adapters.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ You'll need to supply an adapter for each [resource type](https://jsonapi.org/fo
7
7
```php
8
8
use Tobyz\JsonApiServer\Schema\Type;
9
9
10
-
$api->resource('users', $adapter, function (Type $type) {
10
+
$api->resourceType('users', $adapter, function (Type $type) {
11
11
// define your schema
12
12
});
13
13
```
@@ -26,10 +26,10 @@ $adapter = new EloquentAdapter(User::class);
26
26
When using the Eloquent Adapter, the `$model` passed around in the schema will be an instance of the given model, and the `$query` will be a `Illuminate\Database\Eloquent\Builder` instance querying the model's table:
[Extensions](https://jsonapi.org/format/1.1/#extensions) allow your API to support additional functionality that is not part of the base specification.
4
+
5
+
## Defining Extensions
6
+
7
+
Extensions can be defined by extending the `Tobyz\JsonApiServer\Extension\Extension` class and implementing two methods: `uri` and `process`.
8
+
9
+
You must return your extension's unique URI from `uri`.
10
+
11
+
For every request that includes your extension in the media type, the `handle` method will be called. If your extension is able to handle the request, it should return a PSR-7 response. Otherwise, return null to let the normal handling of the request take place.
12
+
13
+
```php
14
+
use Tobyz\JsonApiServer\Extension\Extension;
15
+
use Psr\Http\Message\ResponseInterface;
16
+
17
+
use function Tobyz\JsonApiServer\json_api_response;
18
+
19
+
class MyExtension extends Extension
20
+
{
21
+
public function uri(): string
22
+
{
23
+
return 'https://example.org/my-extension';
24
+
}
25
+
26
+
public function handle(Context $context): ?ResponseInterface;
27
+
{
28
+
if ($context->getPath() === '/my-extension') {
29
+
return json_api_response([
30
+
'my-extension:greeting' => 'Hello world!'
31
+
]);
32
+
}
33
+
34
+
return null;
35
+
}
36
+
}
37
+
```
38
+
39
+
::: warning
40
+
The current implementation of extensions has no support for augmentation of standard API responses. This API may change dramatically in the future. Please [create an issue](https://github.com/tobyzerner/json-api-server/issues/new) if you have a specific use-case you want to achieve.
41
+
:::
42
+
43
+
## Registering Extensions
44
+
45
+
Extensions can be registered on your `JsonApi` instance using the `extension` method:
46
+
47
+
```php
48
+
use Tobyz\JsonApiServer\JsonApi;
49
+
50
+
$api = new JsonApi('/api');
51
+
52
+
$api->extension(new MyExtension());
53
+
```
54
+
55
+
The `JsonApi` class will automatically perform appropriate [content negotiation](https://jsonapi.org/format/1.1/#content-negotiation-servers) and activate the specified extensions on each request.
56
+
57
+
## Atomic Operations
58
+
59
+
An implementation of the [Atomic Operations](https://jsonapi.org/ext/atomic/) extension is available at `Tobyz\JsonApi\Extension\Atomic`.
60
+
61
+
When using this extension, you are responsible for wrapping the `$api->handle` call in a transaction to ensure any database (or other) operations performed are actually atomic in nature. For example, in Laravel:
Copy file name to clipboardExpand all lines: docs/index.md
+10-8Lines changed: 10 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
json-api-server is a [JSON:API](http://jsonapi.org) server implementation in PHP.
4
4
5
-
It allows you to define your API's schema, and then use an [adapter](adapters.md) to connect it to your application's models and database layer, without having to worry about any of the server boilerplate, routing, query parameters, or JSON:API document formatting.
5
+
It allows you to define your API's schema, and then use an [adapter](adapters.md) to connect it to your application's database layer. You don't have to worry about any of the server boilerplate, routing, query parameters, or JSON:API document formatting.
6
6
7
7
Based on your schema definition, the package will serve a **complete JSON:API that conforms to the [spec](https://jsonapi.org/format/)**, including support for:
8
8
@@ -15,7 +15,7 @@ Based on your schema definition, the package will serve a **complete JSON:API th
The schema definition is extremely powerful and lets you easily apply [permissions](visibility.md), [transformations](writing.md#transformers), [validation](writing.md#validation), and custom [filtering](filtering.md) and [sorting](sorting.md) logic to build a fully functional API in minutes.
18
+
The schema definition is extremely powerful and lets you easily apply [permissions](visibility.md), [transformations](writing.md#transformers), [validation](writing.md#validation), and custom [filtering](filtering.md) and [sorting](sorting.md) logic to build a fully functional API with ease.
19
19
20
20
### Example
21
21
@@ -25,25 +25,26 @@ The following example uses Eloquent models in a Laravel application. However, js
25
25
use App\Models\{Article, Comment, User};
26
26
use Tobyz\JsonApiServer\JsonApi;
27
27
use Tobyz\JsonApiServer\Schema\Type;
28
-
use Tobyz\JsonApiServer\Laravel\EloquentAdapter;
28
+
use Tobyz\JsonApiServer\Adapter\EloquentAdapter;
29
29
use Tobyz\JsonApiServer\Laravel;
30
30
31
31
$api = new JsonApi('http://example.com/api');
32
32
33
-
$api->resource('articles', new EloquentAdapter(Article::class), function (Type $type) {
33
+
$api->resourceType('articles', new EloquentAdapter(Article::class), function (Type $type) {
34
34
$type->attribute('title')
35
35
->writable()
36
36
->validate(Laravel\rules('required'));
37
37
38
-
$type->hasOne('author')->type('users')
38
+
$type->hasOne('author')
39
+
->type('users')
39
40
->includable()
40
41
->filterable();
41
42
42
43
$type->hasMany('comments')
43
44
->includable();
44
45
});
45
46
46
-
$api->resource('comments', new EloquentAdapter(Comment::class), function (Type $type) {
47
+
$api->resourceType('comments', new EloquentAdapter(Comment::class), function (Type $type) {
47
48
$type->creatable(Laravel\authenticated());
48
49
$type->updatable(Laravel\can('update-comment'));
49
50
$type->deletable(Laravel\can('delete-comment'));
@@ -56,12 +57,13 @@ $api->resource('comments', new EloquentAdapter(Comment::class), function (Type $
56
57
->writable()->once()
57
58
->validate(Laravel\rules('required'));
58
59
59
-
$type->hasOne('author')->type('users')
60
+
$type->hasOne('author')
61
+
->type('users')
60
62
->writable()->once()
61
63
->validate(Laravel\rules('required'));
62
64
});
63
65
64
-
$api->resource('users', new EloquentAdapter(User::class), function (Type $type) {
66
+
$api->resourceType('users', new EloquentAdapter(User::class), function (Type $type) {
Pass a string or array of validation rules to be applied to the value. Validating array contents is also supported:
19
+
20
+
```php
21
+
$type->attribute('jobs')
22
+
->validate(Laravel\rules([
23
+
'required', 'array',
24
+
'*' => ['string', 'min:3', 'max:255']
25
+
]));
14
26
```
15
27
16
-
Pass a string or array of validation rules to be applied to the value. You can also pass an array of custom messages and custom attribute names as the second and third arguments.
28
+
You can also pass an array of custom messages and custom attribute names as the second and third arguments.
0 commit comments