Skip to content

Commit 7e049f3

Browse files
author
Thibaudeau Pierre
committed
docs: 2.7 update
review @soyuka review @dunglas
1 parent 802f3da commit 7e049f3

39 files changed

+898
-1176
lines changed

admin/handling-relations.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,9 @@ This API uses the following PHP code:
150150
```php
151151
<?php
152152
// api/src/Entity/Review.php
153-
154153
namespace App\Entity;
155154

156-
use ApiPlatform\Core\Annotation\ApiResource;
155+
use ApiPlatform\Metadata\ApiResource;
157156
use Doctrine\ORM\Mapping as ORM;
158157

159158
/**
@@ -179,11 +178,10 @@ class Review
179178
```php
180179
<?php
181180
// api/src/Entity/Book.php
182-
183181
namespace App\Entity;
184182

185183
use ApiPlatform\Core\Annotation\ApiFilter;
186-
use ApiPlatform\Core\Annotation\ApiResource;
184+
use ApiPlatform\Metadata\ApiResource;
187185
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
188186
use Doctrine\Common\Collections\ArrayCollection;
189187
use Doctrine\ORM\Mapping as ORM;

admin/performance.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@ you can make sure the authors are retrieved in one go by writing:
1717
```php
1818
<?php
1919
// api/src/Entity/Author.php
20-
2120
namespace App\Entity;
2221

2322
use ApiPlatform\Core\Annotation\ApiFilter;
24-
use ApiPlatform\Core\Annotation\ApiResource;
2523
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
24+
use ApiPlatform\Metadata\ApiResource;
2625
use Doctrine\ORM\Mapping as ORM;
2726

2827
/**
@@ -35,8 +34,8 @@ class Author
3534
* @ORM\Column(type="integer")
3635
* @ORM\GeneratedValue
3736
* @ORM\Id
38-
* @ApiFilter(SearchFilter::class, strategy="exact")
3937
*/
38+
#[ApiFilter(SearchFilter::class, strategy: "exact")]
4039
public $id;
4140

4241
/**

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: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@ For instance, with API Platform Core as backend, if you write the following:
1212
```php
1313
<?php
1414
// api/src/Entity/Book.php
15+
namespace App\Entity;
1516

16-
use ApiPlatform\Core\Annotation\ApiResource;
17+
use ApiPlatform\Metadata\ApiResource;
1718
use Symfony\Component\Validator\Constraints as Assert;
1819

19-
/**
20-
* @ApiResource
21-
*/
20+
#[ApiResource]
2221
class Book
2322
{
2423
/**
@@ -47,13 +46,12 @@ For example if you have this code:
4746
```php
4847
<?php
4948
// api/src/Entity/Book.php
49+
namespace App\Entity;
5050

51-
use ApiPlatform\Core\Annotation\ApiResource;
51+
use ApiPlatform\Metadata\ApiResource;
5252
use Symfony\Component\Validator\Constraints as Assert;
5353

54-
/**
55-
* @ApiResource
56-
*/
54+
#[ApiResource]
5755
class Book
5856
{
5957
/**

core/content-negotiation.md

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,11 @@ 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-
106105
namespace App\Entity;
107106
108-
use ApiPlatform\Core\Annotation\ApiResource;
107+
use ApiPlatform\Metadata\ApiResource;
109108
110-
#[ApiResource(formats: ['xml', 'jsonld', 'csv' => ['text/csv']])]
109+
#[ApiResource(formats: ['xml', 'jsonld', 'csv' => ['text/csv']])]
111110
class Book
112111
{
113112
// ...
@@ -127,21 +126,17 @@ You can specify different accepted formats at operation level too, it's especial
127126
```php
128127
<?php
129128
// api/src/Entity/Book.php
130-
131129
namespace App\Entity;
132130
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-
)]
131+
use ApiPlatform\Metadata\ApiResource;
132+
use ApiPlatform\Metadata\Patch;
133+
use ApiPlatform\Metadata\GetCollection;
134+
use ApiPlatform\Metadata\Post;
135+
136+
#[ApiResource(formats: ['jsonld', 'csv' => ['text/csv']])]
137+
#[Patch(inputFormats: ['json' => ['application/merge-patch+json']])]
138+
#[GetCollection]
139+
#[Post]
145140
class Book
146141
{
147142
// ...
@@ -227,7 +222,6 @@ services:
227222
```php
228223
<?php
229224
// api/src/Serializer/CustomItemNormalizer.php
230-
231225
namespace App\Serializer;
232226
233227
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
@@ -274,7 +268,6 @@ flatten or remove overly complex relations:
274268
```php
275269
<?php
276270
// api/src/Serializer/CustomItemNormalizer.php
277-
278271
namespace App\Serializer;
279272
280273
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;

core/controllers.md

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ First, let's create your custom operation:
3131
```php
3232
<?php
3333
// api/src/Controller/CreateBookPublication.php
34-
3534
namespace App\Controller;
3635

3736
use App\Entity\Book;
@@ -90,18 +89,20 @@ The routing has not been configured yet because we will add it at the resource c
9089
```php
9190
<?php
9291
// api/src/Entity/Book.php
92+
namespace App\Entity;
9393

94-
use ApiPlatform\Core\Annotation\ApiResource;
94+
use ApiPlatform\Metadata\ApiResource;
95+
use ApiPlatform\Metadata\Get;
96+
use ApiPlatform\Metadata\Post;
9597
use App\Controller\CreateBookPublication;
9698

97-
#[ApiResource(itemOperations: [
98-
'get',
99-
'post_publication' => [
100-
'method' => 'POST',
101-
'path' => '/books/{id}/publication',
102-
'controller' => CreateBookPublication::class,
103-
],
104-
])]
99+
#[ApiResource]
100+
#[Get]
101+
#[Post(
102+
name: 'publication',
103+
uriTemplate: '/books/{id}/publication',
104+
controller: CreateBookPublication::class
105+
)]
105106
class Book
106107
{
107108
// ...
@@ -155,20 +156,22 @@ You may want different serialization groups for your custom operations. Just con
155156
```php
156157
<?php
157158
// api/src/Entity/Book.php
159+
namespace App\Entity;
158160

159-
use ApiPlatform\Core\Annotation\ApiResource;
161+
use ApiPlatform\Metadata\ApiResource;
162+
use ApiPlatform\Metadata\Get;
163+
use ApiPlatform\Metadata\Post;
160164
use App\Controller\CreateBookPublication;
161165
use Symfony\Component\Serializer\Annotation\Groups;
162166

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-
])]
167+
#[ApiResource]
168+
#[Get]
169+
#[Post(
170+
name: 'publication',
171+
uriTemplate: '/books/{id}/publication',
172+
controller: CreateBookPublication::class,
173+
normalizationContext: ['groups' => 'publication']
174+
)]
172175
class Book
173176
{
174177
// ...
@@ -231,19 +234,21 @@ operation attribute:
231234
```php
232235
<?php
233236
// api/src/Entity/Book.php
237+
namespace App\Entity;
234238

235-
use ApiPlatform\Core\Annotation\ApiResource;
239+
use ApiPlatform\Metadata\ApiResource;
240+
use ApiPlatform\Metadata\Get;
241+
use ApiPlatform\Metadata\Post;
236242
use App\Controller\CreateBookPublication;
237243

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-
])]
244+
#[ApiResource]
245+
#[Get]
246+
#[Post(
247+
name: 'publication',
248+
uriTemplate: '/books/{id}/publication',
249+
controller: CreateBookPublication::class,
250+
read: false
251+
)]
247252
class Book
248253
{
249254
// ...
@@ -309,14 +314,16 @@ First, let's create your resource configuration:
309314
```php
310315
<?php
311316
// api/src/Entity/Book.php
317+
namespace App\Entity;
312318

313-
use ApiPlatform\Core\Annotation\ApiResource;
319+
use ApiPlatform\Metadata\ApiResource;
320+
use ApiPlatform\Metadata\Get;
321+
use ApiPlatform\Metadata\Post;
314322

315-
#[ApiResource(itemOperations: [
316-
'get',
317-
'post_publication' => ['route_name' => 'book_post_publication'],
318-
'book_post_discontinuation',
319-
])]
323+
#[ApiResource]
324+
#[Get]
325+
#[Post(name: 'publication', routeName: 'book_post_publication')]
326+
#[Post(name: 'book_post_discontinuation')]
320327
class Book
321328
{
322329
// ...
@@ -361,7 +368,6 @@ and its related route using annotations:
361368
```php
362369
<?php
363370
// api/src/Controller/CreateBookPublication.php
364-
365371
namespace App\Controller;
366372

367373
use App\Entity\Book;
@@ -406,7 +412,6 @@ the same thing as the previous example:
406412
```php
407413
<?php
408414
// api/src/Controller/BookController.php
409-
410415
namespace App\Controller;
411416

412417
use App\Entity\Book;

core/data-providers.md

Lines changed: 1 addition & 3 deletions
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

@@ -266,8 +266,6 @@ using it within your data provider.
266266
<?php
267267
// api/src/DataProvider/CustomCollectionDataProvider.php
268268
269-
declare(strict_types=1);
270-
271269
namespace App\DataProvider;
272270
273271
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;

core/default-order.md

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@ customize this order, you must add an `order` attribute on your ApiResource anno
1212
// api/src/Entity/Book.php
1313
namespace App\Entity;
1414

15-
use ApiPlatform\Core\Annotation\ApiResource;
15+
use ApiPlatform\Metadata\ApiResource;
1616

17-
/**
18-
* @ApiResource(attributes={"order"={"foo": "ASC"}})
19-
*/
17+
#[ApiResource(order: ['foo' => 'ASC'])]
2018
class Book
2119
{
2220
// ...
@@ -50,11 +48,9 @@ If you only specify the key, `ASC` direction will be used as default. For exampl
5048
// api/src/Entity/Book.php
5149
namespace App\Entity;
5250
53-
use ApiPlatform\Core\Annotation\ApiResource;
51+
use ApiPlatform\Metadata\ApiResource;
5452
55-
/**
56-
* @ApiResource(attributes={"order"={"foo", "bar"}})
57-
*/
53+
#[ApiResource(order: ['foo' => 'bar'])]
5854
class Book
5955
{
6056
// ...
@@ -91,11 +87,9 @@ It's also possible to configure the default order on an association property:
9187
// api/src/Entity/Book.php
9288
namespace App\Entity;
9389
94-
use ApiPlatform\Core\Annotation\ApiResource;
90+
use ApiPlatform\Metadata\ApiResource;
9591
96-
/**
97-
* @ApiResource(attributes={"order"={"author.username"}})
98-
*/
92+
#[ApiResource(order: ['author.username'])]
9993
class Book
10094
{
10195
// ...
@@ -123,13 +117,17 @@ Another possibility is to apply the default order for a specific collection oper
123117
[codeSelector]
124118

125119
```php
126-
/**
127-
* collectionOperations={
128-
* "get",
129-
* "get_desc_custom"={"method"="GET", "path"="custom_collection_desc_foos", "order"={"name"="DESC"}},
130-
* "get_asc_custom"={"method"="GET", "path"="custom_collection_asc_foos", "order"={ "name"="ASC"}},
131-
* }
132-
*/
120+
<?php
121+
// api/src/Entity/Book.php
122+
namespace App\Entity;
123+
124+
use ApiPlatform\Metadata\GetCollection;
125+
use ApiPlatform\Metadata\ApiResource;
126+
127+
#[ApiResource]
128+
#[GetCollection]
129+
#[GetCollection(name: 'desc_custom', uriTemplate: 'custom_collection_desc_foos', order: ['name' => 'DESC'])]
130+
#[GetCollection(name: 'asc_custom', uriTemplate: 'custom_collection_asc_foos', order: ['name' => 'ASC'])]
133131
class Book
134132
{
135133
// ...

0 commit comments

Comments
 (0)