Skip to content

fix: remove duplicated documentation after bad merge #1439

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 1 commit into from
Oct 1, 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
9 changes: 3 additions & 6 deletions core/controllers.md
Original file line number Diff line number Diff line change
Expand Up @@ -369,12 +369,9 @@ use Symfony\Component\Routing\Annotation\Route;
#[AsController]
class CreateBookPublication extends AbstractController
{
private $bookPublishingHandler;

public function __construct(BookPublishingHandler $bookPublishingHandler)
{
$this->bookPublishingHandler = $bookPublishingHandler;
}
public function __construct(
private BookPublishingHandler $bookPublishingHandler
) {}

#[Route(
name: 'book_post_publication',
Expand Down
91 changes: 2 additions & 89 deletions core/operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -379,11 +379,6 @@ class Book
App\Entity\Book:
attributes:
route_prefix: /library
itemOperations:
get: ~
post_publication:
route_name: book_post_publication
book_post_discontinuation: ~
```

```xml
Expand All @@ -395,13 +390,7 @@ App\Entity\Book:
xsi:schemaLocation="https://api-platform.com/schema/metadata
https://api-platform.com/schema/metadata/metadata-2.0.xsd">
<resource class="App\Entity\Book">
<itemOperations>
<itemOperation name="get" />
<itemOperation name="post_publication">
<attribute name="route_name">book_post_publication</attribute>
</itemOperation>
<itemOperation name="book_post_discontinuation" />
</itemOperations>
<attribute name="route_prefix">/library</attribute>
</resource>
</resources>
```
Expand All @@ -410,83 +399,7 @@ App\Entity\Book:

Alternatively, the more verbose attribute syntax can be used: `@ApiResource(attributes={"route_prefix"="/library"})`.

API Platform will automatically map this `post_publication` operation to the route `book_post_publication`. Let's create a custom action
and its related route using annotations:

```php
<?php
// api/src/Controller/CreateBookPublication.php

namespace App\Controller;

use App\Entity\Book;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\Routing\Annotation\Route;

#[AsController]
class CreateBookPublication extends AbstractController
{
public function __construct(
private BookPublishingHandler $bookPublishingHandler
) {}

#[Route(
path: '/books/{id}/publication',
name: 'book_post_publication',
defaults: [
'_api_resource_class' => Book::class,
'_api_item_operation_name' => 'post_publication',
],
methods: ['POST'],
)]
public function __invoke(Book $data): Book
{
$this->bookPublishingHandler->handle($data);

return $data;
}
}
```

It is mandatory to set `_api_resource_class` and `_api_item_operation_name` (or `_api_collection_operation_name` for a collection
operation) in the parameters of the route (`defaults` key). It allows API Platform to work with the Symfony routing system.

Alternatively, you can also use a traditional Symfony controller and YAML or XML route declarations. The following example does
the same thing as the previous example:

```php
<?php
// api/src/Controller/BookController.php

namespace App\Controller;

use App\Entity\Book;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpKernel\Attribute\AsController;

#[AsController]
class BookController extends AbstractController
{
public function createPublication(Book $data, BookPublishingHandler $bookPublishingHandler): Book
{
return $bookPublishingHandler->handle($data);
}
}
```

```yaml
# api/config/routes.yaml
book_post_publication:
path: /books/{id}/publication
methods: ['POST']
defaults:
_controller: App\Controller\BookController::createPublication
_api_resource_class: App\Entity\Book
_api_item_operation_name: post_publication
```

## Expose a model without any routes
## Expose a Model Without Any Routes

Sometimes, you may want to expose a model, but want it to be used through subrequests only, and never through item or collection operations.
Because the OpenAPI standard requires at least one route to be exposed to make your models consumable, let's see how you can manage this kind
Expand Down