Skip to content

docs: 2.7 update #1422

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions admin/handling-relations.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,9 @@ This API uses the following PHP code:
```php
<?php
// api/src/Entity/Review.php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Metadata\ApiResource;
use Doctrine\ORM\Mapping as ORM;

/**
Expand All @@ -179,11 +178,10 @@ class Review
```php
<?php
// api/src/Entity/Book.php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
Expand Down
5 changes: 2 additions & 3 deletions admin/performance.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ you can make sure the authors are retrieved in one go by writing:
```php
<?php
// api/src/Entity/Author.php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Metadata\ApiResource;
use Doctrine\ORM\Mapping as ORM;

/**
Expand All @@ -35,8 +34,8 @@ class Author
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
* @ORM\Id
* @ApiFilter(SearchFilter::class, strategy="exact")
*/
#[ApiFilter(SearchFilter::class, strategy: "exact")]
public $id;

/**
Expand Down
7 changes: 4 additions & 3 deletions admin/schema.org.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ To configure which property should be shown to represent your entity, map the pr

```php
// api/src/Entity/Person.php
...

/**
* @ApiProperty(iri="http://schema.org/name")
*/
#[ApiProperty(types: ["http://schema.org/name"])]
private $name;

...
```

## Emails, URLs and Identifiers
Expand Down
14 changes: 6 additions & 8 deletions admin/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ For instance, with API Platform Core as backend, if you write the following:
```php
<?php
// api/src/Entity/Book.php
namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Metadata\ApiResource;
use Symfony\Component\Validator\Constraints as Assert;

/**
* @ApiResource
*/
#[ApiResource]
class Book
{
/**
Expand Down Expand Up @@ -47,13 +46,12 @@ For example if you have this code:
```php
<?php
// api/src/Entity/Book.php
namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Metadata\ApiResource;
use Symfony\Component\Validator\Constraints as Assert;

/**
* @ApiResource
*/
#[ApiResource]
class Book
{
/**
Expand Down
29 changes: 11 additions & 18 deletions core/content-negotiation.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,11 @@ The `format` attribute can be used as a shortcut, it sets both the `input_format
```php
<?php
// api/src/Entity/Book.php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Metadata\ApiResource;

#[ApiResource(formats: ['xml', 'jsonld', 'csv' => ['text/csv']])]
#[ApiResource(formats: ['xml', 'jsonld', 'csv' => ['text/csv']])]
class Book
{
// ...
Expand All @@ -127,21 +126,17 @@ You can specify different accepted formats at operation level too, it's especial
```php
<?php
// api/src/Entity/Book.php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;

#[ApiResource(
formats: ['jsonld', 'csv' => ['text/csv']],
itemOperations: [
'patch' => [
'input_formats' => [
'json' => ['application/merge-patch+json'],
],
],
],
)]
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Post;

#[ApiResource(formats: ['jsonld', 'csv' => ['text/csv']])]
#[Patch(inputFormats: ['json' => ['application/merge-patch+json']])]
#[GetCollection]
#[Post]
class Book
{
// ...
Expand Down Expand Up @@ -227,7 +222,6 @@ services:
```php
<?php
// api/src/Serializer/CustomItemNormalizer.php

namespace App\Serializer;

use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
Expand Down Expand Up @@ -274,7 +268,6 @@ flatten or remove overly complex relations:
```php
<?php
// api/src/Serializer/CustomItemNormalizer.php

namespace App\Serializer;

use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
Expand Down
81 changes: 43 additions & 38 deletions core/controllers.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ First, let's create your custom operation:
```php
<?php
// api/src/Controller/CreateBookPublication.php

namespace App\Controller;

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

use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Post;
use App\Controller\CreateBookPublication;

#[ApiResource(itemOperations: [
'get',
'post_publication' => [
'method' => 'POST',
'path' => '/books/{id}/publication',
'controller' => CreateBookPublication::class,
],
])]
#[ApiResource]
#[Get]
#[Post(
name: 'publication',
uriTemplate: '/books/{id}/publication',
controller: CreateBookPublication::class
)]
class Book
{
// ...
Expand Down Expand Up @@ -155,20 +156,22 @@ You may want different serialization groups for your custom operations. Just con
```php
<?php
// api/src/Entity/Book.php
namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Post;
use App\Controller\CreateBookPublication;
use Symfony\Component\Serializer\Annotation\Groups;

#[ApiResource(itemOperations: [
'get',
'post_publication' => [
'method' => 'POST',
'path' => '/books/{id}/publication',
'controller' => CreateBookPublication::class,
'normalization_context' => ['groups' => 'publication'],
],
])]
#[ApiResource]
#[Get]
#[Post(
name: 'publication',
uriTemplate: '/books/{id}/publication',
controller: CreateBookPublication::class,
normalizationContext: ['groups' => 'publication']
)]
class Book
{
// ...
Expand Down Expand Up @@ -231,19 +234,21 @@ operation attribute:
```php
<?php
// api/src/Entity/Book.php
namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Post;
use App\Controller\CreateBookPublication;

#[ApiResource(itemOperations: [
'get',
'post_publication' => [
'method' => 'POST',
'path' => '/books/{id}/publication',
'controller' => CreateBookPublication::class,
'read' => false,
],
])]
#[ApiResource]
#[Get]
#[Post(
name: 'publication',
uriTemplate: '/books/{id}/publication',
controller: CreateBookPublication::class,
read: false
)]
class Book
{
// ...
Expand Down Expand Up @@ -309,14 +314,16 @@ First, let's create your resource configuration:
```php
<?php
// api/src/Entity/Book.php
namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Post;

#[ApiResource(itemOperations: [
'get',
'post_publication' => ['route_name' => 'book_post_publication'],
'book_post_discontinuation',
])]
#[ApiResource]
#[Get]
#[Post(name: 'publication', routeName: 'book_post_publication')]
#[Post(name: 'book_post_discontinuation')]
class Book
{
// ...
Expand Down Expand Up @@ -361,7 +368,6 @@ and its related route using annotations:
```php
<?php
// api/src/Controller/CreateBookPublication.php

namespace App\Controller;

use App\Entity\Book;
Expand Down Expand Up @@ -406,7 +412,6 @@ the same thing as the previous example:
```php
<?php
// api/src/Controller/BookController.php

namespace App\Controller;

use App\Entity\Book;
Expand Down
3 changes: 3 additions & 0 deletions core/data-persisters.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ persist data for a given resource will be used.
## Creating a Custom Data Persister

If the [Symfony MakerBundle](https://symfony.com/doc/current/bundles/SymfonyMakerBundle) is installed in your project, you can use the following command to generate a custom data persister easily:

```console
bin/console make:data-persister
```
Expand Down Expand Up @@ -190,6 +191,7 @@ final class BlogPostDataPersister implements ContextAwareDataPersisterInterface,
```

This is very useful when using [`Messenger` with API Platform](messenger.md) as you may want to do something asynchronously with the data but still call the default Doctrine data persister, for example:

```php
namespace App\DataPersister;

Expand Down Expand Up @@ -230,6 +232,7 @@ final class BlogPostDataPersister implements ContextAwareDataPersisterInterface,
}
}
```

```yaml
# api/config/services.yaml
services:
Expand Down
6 changes: 3 additions & 3 deletions core/data-providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ Both implementations can also implement a third, optional, interface called
if you want to limit their effects to a single resource or operation.

In the following examples we will create custom data providers for an entity class called `App\Entity\BlogPost`.
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)).
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)).

## Custom Collection Data Provider

If the [Symfony MakerBundle](https://symfony.com/doc/current/bundles/SymfonyMakerBundle) is installed in your project, you can use the following command to generate a custom collection data provider easily:

```console
bin/console make:data-provider --collection-only
```
Expand Down Expand Up @@ -85,6 +86,7 @@ You can find a full working example in the [API Platform's demo application](htt
## Custom Item Data Provider

If the [Symfony MakerBundle](https://symfony.com/doc/current/bundles/SymfonyMakerBundle) is installed in your project, you can use the following command to generate a custom item data provider easily:

```console
bin/console make:data-provider --item-only
```
Expand Down Expand Up @@ -266,8 +268,6 @@ using it within your data provider.
<?php
// api/src/DataProvider/CustomCollectionDataProvider.php

declare(strict_types=1);

namespace App\DataProvider;

use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
Expand Down
Loading