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/api/rest_api/extending_rest_api/creating_new_rest_resource.md
+49-47Lines changed: 49 additions & 47 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,8 +8,8 @@ To create a new REST resource, you need to prepare:
8
8
9
9
- the REST route leading to a controller action
10
10
- the controller and its action
11
-
- one or several `InputParser` objects if the controller needs to receive a payload to treat, one or several value classes to represent this payload and potentially one or several new media types to type this payload in the `Content-Type` header (optional)
12
-
- one or several new value classes to represent the controller action result, their `ValueObjectVisitor` to help the generator to turn this into XML or JSON and potentially one or several new media types to claim in the `Accept` header the desired value (optional)
11
+
- one or several input denormalizers if the controller needs to receive a payload to treat, one or several value classes to represent this payload, and potentially one or several new media types to type this payload in the `Content-Type` header (optional)
12
+
- one or several new value classes to represent the controller action result, their normalizers to help the generator to turn this into XML or JSON, and potentially one or several new media types to claim in the `Accept` header the desired value (optional)
13
13
- the addition of this resource route to the REST root (optional)
14
14
15
15
In the following example, you add a greeting resource to the REST API.
Having the REST controllers set as services enables using features such as the `InputDispatcher` service in the [Controller action](#controller-action).
54
+
Having the REST controllers set as services enables using features such as
55
+
56
+
- TODO: `controller.service_arguments`??
57
+
- TODO: `ibexa.api_platform.resource`tag is needed to have the route available in live doc (/api/ibexa/v2/doc#/App/api_greet_get)
55
58
56
59
### Controller action
57
60
58
61
A REST controller should:
59
62
60
-
- return a value object and have a `Generator` and `ValueObjectVisitor`s producing the XML or JSON output
61
-
- extend `Ibexa\Rest\Server\Controller` to inherit utils methods and properties like `InputDispatcher` or `RequestParser`
63
+
- return an object (passed automatically to a normaliser) or a `Response` (to customize it further)
64
+
- TODO: extend `Ibexa\Rest\Server\Controller` to inherit utils methods and properties like `InputDispatcher` or `RequestParser`
If the returned value was depending on a location, it could have been wrapped in a `CachedValue` to be cached by the reverse proxy (like Varnish) for future calls.
| `$visitor` | The output visitor.<br/>Can be used to set custom response headers (`setHeader`), HTTP status code ( `setStatus`) |
89
-
| `$generator` | The actual response generator. It provides you with a DOM-like API. |
90
-
| `$data` | The visited data. The exact object that you returned from the controller.<br/>It can't have a type declaration because the method signature is shared. |
Here, the media type is `application/vnd.ibexa.api.Greeting` plus a format.
105
-
To have a different vendor than the default, you could create a new `Output\Generator` or hard-code it in the `ValueObjectVisitor` like in the [`RestLocation` example](adding_custom_media_type.md#new-restlocation-valueobjectvisitor).
106
-
107
-
## InputParser
108
-
109
-
A REST resource could use route parameters to handle input, but this example illustrates the usage of an input parser.
110
-
111
-
For this example, the structure is a `GreetingInput` root node with two leaf nodes, `Salutation` and `Recipient`.
99
+
A normalizer must implement the `supportsNormalization` and `normalize` methods.
Here, this `InputParser` directly returns the right value object.
118
-
In other cases, it could return whatever object is needed to represent the input for the controller to perform its action, like arguments to use with a Repository service.
0 commit comments