|
1 | 1 | <?php
|
2 |
| -// --- |
| 2 | +// --- |
3 | 3 | // slug: declare-a-resource
|
4 | 4 | // name: Declare a Resource
|
5 | 5 | // position: 1
|
|
10 | 10 |
|
11 | 11 | // # Declare a Resource
|
12 | 12 | // This class represents an API resource
|
13 |
| -namespace App\ApiResource; |
| 13 | +namespace App\ApiResource { |
| 14 | + // The `#[ApiResource]` attribute registers this class as an HTTP resource. |
| 15 | + use ApiPlatform\Metadata\ApiResource; |
| 16 | + // These are the list of HTTP operations we use to declare a "CRUD" (Create, Read, Update, Delete). |
| 17 | + use ApiPlatform\Metadata\Get; |
| 18 | + use ApiPlatform\Metadata\GetCollection; |
| 19 | + use ApiPlatform\Metadata\Post; |
| 20 | + use ApiPlatform\Metadata\Patch; |
| 21 | + use ApiPlatform\Metadata\Delete; |
| 22 | + use ApiPlatform\Validator\Exception\ValidationException; |
| 23 | + |
| 24 | + // Each resource has its set of Operations. |
| 25 | + // Note that the uriTemplate may use the `id` variable which is our unique |
| 26 | + // identifier on this `Book`. |
| 27 | + #[ApiResource( |
| 28 | + operations: [ |
| 29 | + new Get(uriTemplate: '/books/{id}'), |
| 30 | + // The GetCollection operation returns a list of Books. |
| 31 | + new GetCollection(uriTemplate: '/books'), |
| 32 | + new Post(uriTemplate: '/books'), |
| 33 | + new Patch(uriTemplate: '/books/{id}'), |
| 34 | + new Delete(uriTemplate: '/books/{id}'), |
| 35 | + ], |
| 36 | + // This is a configuration that is shared accross every operations. More details are available at [ApiResource::exceptionToStatus](/reference/Metadata/ApiResource#exceptionToStatus). |
| 37 | + exceptionToStatus: [ |
| 38 | + ValidationException::class => 422 |
| 39 | + ] |
| 40 | + )] |
| 41 | + // If a property named `id` is found it is the property used in your URI template |
| 42 | + // we recommend to use public properties to declare API resources. |
| 43 | + class Book |
| 44 | + { |
| 45 | + public string $id; |
| 46 | + } |
| 47 | +} |
14 | 48 |
|
15 |
| -// The `#[ApiResource]` attribute registers this class as an HTTP resource. |
16 |
| -use ApiPlatform\Metadata\ApiResource; |
17 |
| -// These are the list of HTTP operations we use to declare a "CRUD" (Create, Read, Update, Delete). |
18 |
| -use ApiPlatform\Metadata\Get; |
19 |
| -use ApiPlatform\Metadata\GetCollection; |
20 |
| -use ApiPlatform\Metadata\Post; |
21 |
| -use ApiPlatform\Metadata\Patch; |
22 |
| -use ApiPlatform\Metadata\Delete; |
23 |
| -use ApiPlatform\Validator\Exception\ValidationException; |
| 49 | +// Check our next guide to [provide the resource state](./provide-the-resource-state). |
| 50 | +namespace App\Playground { |
| 51 | + use Symfony\Component\HttpFoundation\Request; |
24 | 52 |
|
25 |
| -// Each resource has its set of Operations. |
26 |
| -// Note that the uriTemplate may use the `id` variable which is our unique |
27 |
| -// identifier on this `Book`. |
28 |
| -#[ApiResource( |
29 |
| - operations: [ |
30 |
| - new Get(uriTemplate: '/books/{id}'), |
31 |
| - // The GetCollection operation returns a list of Books. |
32 |
| - new GetCollection(uriTemplate: '/books'), |
33 |
| - new Post(uriTemplate: '/books'), |
34 |
| - new Patch(uriTemplate: '/books/{id}'), |
35 |
| - new Delete(uriTemplate: '/books/{id}'), |
36 |
| - ], |
37 |
| - // This is a configuration that is shared accross every operations. More details are available at [ApiResource::exceptionToStatus](/reference/Metadata/ApiResource#exceptionToStatus). |
38 |
| - exceptionToStatus: [ |
39 |
| - ValidationException::class => 422 |
40 |
| - ] |
41 |
| -)] |
42 |
| -// If a property named `id` is found it is the property used in your URI template |
43 |
| -// we recommend to use public properties to declare API resources. |
44 |
| -class Book |
45 |
| -{ |
46 |
| - public string $id; |
| 53 | + function request(): Request |
| 54 | + { |
| 55 | + return Request::create('/docs', 'GET'); |
| 56 | + } |
47 | 57 | }
|
48 |
| -// Select the [next example](./hook-a-persistence-layer-with-a-processor) to see how to hook a persistence layer. |
| 58 | + |
0 commit comments