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: core/operations.md
+67-54Lines changed: 67 additions & 54 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -189,7 +189,18 @@ resources:
189
189
API Platform is smart enough to automatically register the applicable Symfony route referencing a built-in CRUD action
190
190
just by specifying the method name as key, or by checking the explicitly configured HTTP method.
191
191
192
-
If you do not want to allow access to the resource item (i.e. you don't want a `GET` item operation), instead of omitting it altogether, you should instead declare a `GET` item operation which returns HTTP 404 (Not Found), so that the resource item can still be identified by an IRI. For example:
192
+
By default, API Platform uses the first `Get` operation defined to generate the IRI of an item and the first `GetCollection` operation to generate the IRI of a collection.
193
+
194
+
If your resource does not have any `Get` operation, API Platform automatically adds an operation to help generating this IRI.
195
+
If your resource has any identifier, this operation will look like `/books/{id}`. But if your resource doesn’t have any identifier, API Platform will use the Skolem format `/.well-known/genid/{id}`.
196
+
Those routes are not exposed from any documentation (for instance OpenAPI), but are anyway declared on the routing system and always return a HTTP 404.
197
+
198
+
## Configuring Operations
199
+
200
+
The URL, the method and the default status code (among other options) can be configured per operation.
201
+
202
+
In the next example, both `GET` and `POST` operations are registered with custom URLs. Those will override the URLs generated by default.
203
+
In addition to that, we require the `id` parameter in the URL of the `GET` operation to be an integer, and we configure the status code generated after successful `POST` request to be `301`:
193
204
194
205
<code-selector>
195
206
@@ -198,22 +209,27 @@ If you do not want to allow access to the resource item (i.e. you don't want a `
The URL, the method and the default status code (among other options) can be configured per operation.
255
-
256
-
In the next example, both `GET` and `POST` operations are registered with custom URLs. Those will override the URLs generated by default.
257
-
In addition to that, we require the `id` parameter in the URL of the `GET` operation to be an integer, and we configure the status code generated after successful `POST` request to be `301`:
292
+
When you do not want to allow access to the resource item (i.e. you don't want a `GET` item operation), instead of omitting the resource item altogether, you can explicitly specify the IRI of the resource item by declaring a `GET` item operation that returns HTTP 404 (Not Found). For example:
258
293
259
294
<code-selector>
260
295
@@ -263,18 +298,17 @@ In addition to that, we require the `id` parameter in the URL of the `GET` opera
263
298
// api/src/Entity/Book.php
264
299
namespace App\Entity;
265
300
301
+
use ApiPlatform\Action\NotFoundAction;
266
302
use ApiPlatform\Metadata\ApiResource;
267
303
use ApiPlatform\Metadata\Get;
268
304
use ApiPlatform\Metadata\Post;
269
305
270
306
#[ApiResource(operations: [
271
307
new Get(
272
308
uriTemplate: '/grimoire/{id}',
273
-
requirements: ['id' => '\d+'],
274
-
defaults: ['color' => 'brown'],
275
-
options: ['my_option' => 'my_option_value'],
276
-
schemes: ['https'],
277
-
host: '{subdomain}.api-platform.com'
309
+
controller: NotFoundAction::class,
310
+
read: false,
311
+
output: false
278
312
),
279
313
new Post(
280
314
uriTemplate: '/grimoire',
@@ -283,7 +317,7 @@ use ApiPlatform\Metadata\Post;
Copy file name to clipboardExpand all lines: symfony/messenger.md
+1-9Lines changed: 1 addition & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -28,13 +28,10 @@ namespace App\Entity;
28
28
29
29
use ApiPlatform\Metadata\ApiResource;
30
30
use ApiPlatform\Metadata\ApiProperty;
31
-
use ApiPlatform\Metadata\Get;
32
31
use ApiPlatform\Metadata\Post;
33
32
use Symfony\Component\Validator\Constraints as Assert;
34
-
use ApiPlatform\Action\NotFoundAction;
35
33
36
34
#[ApiResource(operations: [
37
-
new Get(controller: NotFoundAction::class, read: false, status: 404),
38
35
new Post(messenger: true, output: false, status: 202)
39
36
])]
40
37
final class Person
@@ -56,18 +53,13 @@ resources:
56
53
status: 202
57
54
messenger: true
58
55
output: false
59
-
ApiPlatform\Metadata\Get:
60
-
status: 404
61
-
controller: ApiPlatform\Action\NotFoundAction
62
-
read: false
63
56
```
64
57
65
58
</code-selector>
66
59
67
60
Because the `messenger` attribute is `true`, when a `POST` is handled by API Platform, the corresponding instance of the `Person` will be dispatched.
68
61
69
-
For this example, only the `POST` operation is enabled. We disabled the item operation using the `NotFoundAction`. A resource must have at least one item operation as it must be identified by an IRI, here the route `/people/1` exists, eventhough it returns a 404 status code.
70
-
We use the `status` attribute to configure API Platform to return a [202 Accepted HTTP status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/202).
62
+
For this example, only the `POST` operation is enabled. If the resource does not have any `Get` operation, API Platform [automatically adds an operation to help generating an IRI identify the resource](../core/operations/#enabling-and-disabling-operations). We use the `status` attribute to configure API Platform to return a [202 Accepted HTTP status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/202).
71
63
It indicates that the request has been received and will be treated later, without giving an immediate return to the client.
72
64
Finally, the `output` attribute is set to `false`, so the HTTP response that will be generated by API Platform will be empty, and the [serialization process](../core/serialization.md) will be skipped.
0 commit comments