Skip to content

Commit bb487d9

Browse files
author
Thibaudeau Pierre
committed
docs: 2.7 update
· php8 attributes instead of annotations. · Removes item & collection operations · ApiPlatform\Core\Annotation\ to ApiPlatform\Metadata\ · forgotten file names · adds "declare_strict" at the beginning of php examples
1 parent 802f3da commit bb487d9

38 files changed

+1042
-1023
lines changed

admin/handling-relations.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,11 @@ This API uses the following PHP code:
150150
```php
151151
<?php
152152
// api/src/Entity/Review.php
153+
declare(strict_types=1);
153154

154155
namespace App\Entity;
155156

156-
use ApiPlatform\Core\Annotation\ApiResource;
157+
use ApiPlatform\Metadata\ApiResource;
157158
use Doctrine\ORM\Mapping as ORM;
158159

159160
/**
@@ -179,11 +180,12 @@ class Review
179180
```php
180181
<?php
181182
// api/src/Entity/Book.php
183+
declare(strict_types=1);
182184

183185
namespace App\Entity;
184186

185187
use ApiPlatform\Core\Annotation\ApiFilter;
186-
use ApiPlatform\Core\Annotation\ApiResource;
188+
use ApiPlatform\Metadata\ApiResource;
187189
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
188190
use Doctrine\Common\Collections\ArrayCollection;
189191
use Doctrine\ORM\Mapping as ORM;

admin/performance.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ you can make sure the authors are retrieved in one go by writing:
1717
```php
1818
<?php
1919
// api/src/Entity/Author.php
20+
declare(strict_types=1);
2021

2122
namespace App\Entity;
2223

2324
use ApiPlatform\Core\Annotation\ApiFilter;
24-
use ApiPlatform\Core\Annotation\ApiResource;
2525
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
26+
use ApiPlatform\Metadata\ApiResource;
2627
use Doctrine\ORM\Mapping as ORM;
2728

2829
/**
@@ -35,8 +36,8 @@ class Author
3536
* @ORM\Column(type="integer")
3637
* @ORM\GeneratedValue
3738
* @ORM\Id
38-
* @ApiFilter(SearchFilter::class, strategy="exact")
3939
*/
40+
#[ApiFilter(SearchFilter::class, strategy: "exact")]
4041
public $id;
4142

4243
/**

admin/schema.org.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ To configure which property should be shown to represent your entity, map the pr
1616

1717
```php
1818
// api/src/Entity/Person.php
19+
...
1920

20-
/**
21-
* @ApiProperty(iri="http://schema.org/name")
22-
*/
21+
#[ApiProperty(types: ["http://schema.org/name"])]
2322
private $name;
23+
24+
...
2425
```
2526

2627
## Emails, URLs and Identifiers

admin/validation.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@ For instance, with API Platform Core as backend, if you write the following:
1313
<?php
1414
// api/src/Entity/Book.php
1515

16-
use ApiPlatform\Core\Annotation\ApiResource;
16+
use ApiPlatform\Metadata\ApiResource;
1717
use Symfony\Component\Validator\Constraints as Assert;
1818

19-
/**
20-
* @ApiResource
21-
*/
19+
#[ApiResource]
2220
class Book
2321
{
2422
/**
@@ -48,12 +46,10 @@ For example if you have this code:
4846
<?php
4947
// api/src/Entity/Book.php
5048

51-
use ApiPlatform\Core\Annotation\ApiResource;
49+
use ApiPlatform\Metadata\ApiResource;
5250
use Symfony\Component\Validator\Constraints as Assert;
5351

54-
/**
55-
* @ApiResource
56-
*/
52+
#[ApiResource]
5753
class Book
5854
{
5955
/**

core/content-negotiation.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,13 @@ The `format` attribute can be used as a shortcut, it sets both the `input_format
102102
```php
103103
<?php
104104
// api/src/Entity/Book.php
105+
declare(strict_types=1);
105106
106107
namespace App\Entity;
107108
108-
use ApiPlatform\Core\Annotation\ApiResource;
109+
use ApiPlatform\Metadata\ApiResource;
109110
110-
#[ApiResource(formats: ['xml', 'jsonld', 'csv' => ['text/csv']])]
111+
#[ApiResource(formats: ['xml', 'jsonld', 'csv' => ['text/csv']])]
111112
class Book
112113
{
113114
// ...
@@ -127,21 +128,19 @@ You can specify different accepted formats at operation level too, it's especial
127128
```php
128129
<?php
129130
// api/src/Entity/Book.php
131+
declare(strict_types=1);
130132
131133
namespace App\Entity;
132134
133-
use ApiPlatform\Core\Annotation\ApiResource;
134-
135-
#[ApiResource(
136-
formats: ['jsonld', 'csv' => ['text/csv']],
137-
itemOperations: [
138-
'patch' => [
139-
'input_formats' => [
140-
'json' => ['application/merge-patch+json'],
141-
],
142-
],
143-
],
144-
)]
135+
use ApiPlatform\Metadata\ApiResource;
136+
use ApiPlatform\Metadata\Patch;
137+
use ApiPlatform\Metadata\GetCollection;
138+
use ApiPlatform\Metadata\Post;
139+
140+
#[ApiResource(formats: ['jsonld', 'csv' => ['text/csv']])]
141+
#[Patch(inputFormats: ['json' => ['application/merge-patch+json']])]
142+
#[GetCollection]
143+
#[Post]
145144
class Book
146145
{
147146
// ...
@@ -227,6 +226,7 @@ services:
227226
```php
228227
<?php
229228
// api/src/Serializer/CustomItemNormalizer.php
229+
declare(strict_types=1);
230230
231231
namespace App\Serializer;
232232
@@ -274,6 +274,7 @@ flatten or remove overly complex relations:
274274
```php
275275
<?php
276276
// api/src/Serializer/CustomItemNormalizer.php
277+
declare(strict_types=1);
277278
278279
namespace App\Serializer;
279280

core/controllers.md

Lines changed: 54 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ First, let's create your custom operation:
3131
```php
3232
<?php
3333
// api/src/Controller/CreateBookPublication.php
34+
declare(strict_types=1);
3435

3536
namespace App\Controller;
3637

@@ -90,18 +91,22 @@ The routing has not been configured yet because we will add it at the resource c
9091
```php
9192
<?php
9293
// api/src/Entity/Book.php
94+
declare(strict_types=1);
9395

94-
use ApiPlatform\Core\Annotation\ApiResource;
96+
namespace App\Entity;
97+
98+
use ApiPlatform\Metadata\ApiResource;
99+
use ApiPlatform\Metadata\Get;
100+
use ApiPlatform\Metadata\Post;
95101
use App\Controller\CreateBookPublication;
96102

97-
#[ApiResource(itemOperations: [
98-
'get',
99-
'post_publication' => [
100-
'method' => 'POST',
101-
'path' => '/books/{id}/publication',
102-
'controller' => CreateBookPublication::class,
103-
],
104-
])]
103+
#[ApiResource]
104+
#[Get]
105+
#[Post(
106+
name: 'post_publication',
107+
uriTemplate: '/books/{id}/publication',
108+
controller: CreateBookPublication::class
109+
)]
105110
class Book
106111
{
107112
// ...
@@ -155,20 +160,24 @@ You may want different serialization groups for your custom operations. Just con
155160
```php
156161
<?php
157162
// api/src/Entity/Book.php
163+
declare(strict_types=1);
164+
165+
namespace App\Entity;
158166

159-
use ApiPlatform\Core\Annotation\ApiResource;
167+
use ApiPlatform\Metadata\ApiResource;
168+
use ApiPlatform\Metadata\Get;
169+
use ApiPlatform\Metadata\Post;
160170
use App\Controller\CreateBookPublication;
161171
use Symfony\Component\Serializer\Annotation\Groups;
162172

163-
#[ApiResource(itemOperations: [
164-
'get',
165-
'post_publication' => [
166-
'method' => 'POST',
167-
'path' => '/books/{id}/publication',
168-
'controller' => CreateBookPublication::class,
169-
'normalization_context' => ['groups' => 'publication'],
170-
],
171-
])]
173+
#[ApiResource]
174+
#[Get]
175+
#[Post(
176+
name: 'post_publication',
177+
uriTemplate: '/books/{id}/publication',
178+
controller: CreateBookPublication::class,
179+
normalizationContext: ['groups' => 'publication']
180+
)]
172181
class Book
173182
{
174183
// ...
@@ -231,19 +240,23 @@ operation attribute:
231240
```php
232241
<?php
233242
// api/src/Entity/Book.php
243+
declare(strict_types=1);
234244

235-
use ApiPlatform\Core\Annotation\ApiResource;
245+
namespace App\Entity;
246+
247+
use ApiPlatform\Metadata\ApiResource;
248+
use ApiPlatform\Metadata\Get;
249+
use ApiPlatform\Metadata\Post;
236250
use App\Controller\CreateBookPublication;
237251

238-
#[ApiResource(itemOperations: [
239-
'get',
240-
'post_publication' => [
241-
'method' => 'POST',
242-
'path' => '/books/{id}/publication',
243-
'controller' => CreateBookPublication::class,
244-
'read' => false,
245-
],
246-
])]
252+
#[ApiResource]
253+
#[Get]
254+
#[Post(
255+
name: 'post_publication',
256+
uriTemplate: '/books/{id}/publication',
257+
controller: CreateBookPublication::class,
258+
read: false
259+
)]
247260
class Book
248261
{
249262
// ...
@@ -309,14 +322,18 @@ First, let's create your resource configuration:
309322
```php
310323
<?php
311324
// api/src/Entity/Book.php
325+
declare(strict_types=1);
326+
327+
namespace App\Entity;
312328

313-
use ApiPlatform\Core\Annotation\ApiResource;
329+
use ApiPlatform\Metadata\ApiResource;
330+
use ApiPlatform\Metadata\Get;
331+
use ApiPlatform\Metadata\Post;
314332

315-
#[ApiResource(itemOperations: [
316-
'get',
317-
'post_publication' => ['route_name' => 'book_post_publication'],
318-
'book_post_discontinuation',
319-
])]
333+
#[ApiResource]
334+
#[Get]
335+
#[Post(name: 'post_publication', routeName: 'book_post_publication')]
336+
#[Post(name: 'book_post_discontinuation')]
320337
class Book
321338
{
322339
// ...
@@ -361,6 +378,7 @@ and its related route using annotations:
361378
```php
362379
<?php
363380
// api/src/Controller/CreateBookPublication.php
381+
declare(strict_types=1);
364382

365383
namespace App\Controller;
366384

@@ -406,6 +424,7 @@ the same thing as the previous example:
406424
```php
407425
<?php
408426
// api/src/Controller/BookController.php
427+
declare(strict_types=1);
409428

410429
namespace App\Controller;
411430

core/data-providers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Both implementations can also implement a third, optional, interface called
2323
if you want to limit their effects to a single resource or operation.
2424

2525
In the following examples we will create custom data providers for an entity class called `App\Entity\BlogPost`.
26-
Note, that if your entity is not Doctrine-related, you need to flag the identifier property by using `@ApiProperty(identifier=true)` for things to work properly (see also [Entity Identifier Case](serialization.md#entity-identifier-case)).
26+
Note, that if your entity is not Doctrine-related, you need to flag the identifier property by using `#[ApiProperty(identifier: true)]` for things to work properly (see also [Entity Identifier Case](serialization.md#entity-identifier-case)).
2727

2828
## Custom Collection Data Provider
2929

0 commit comments

Comments
 (0)