Skip to content

Fix: Remove duplicated docs and fix constructors #1702

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 2 commits into from
Jan 24, 2023
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 @@ -44,12 +44,9 @@ use Symfony\Component\HttpKernel\Attribute\AsController;
#[AsController]
class CreateBookPublication extends AbstractController
{
private $bookPublishingHandler;

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

public function __invoke(Book $book): Book
{
Expand Down
75 changes: 0 additions & 75 deletions core/operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -365,81 +365,6 @@ App\Entity\Book:

[/codeSelector]

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_operation_name' => '_api_/books/{id}/publication_post',
],
methods: ['POST'],
)]
public function __invoke(Book $book): Book
{
$this->bookPublishingHandler->handle($book);

return $book;
}
}
```

It is mandatory to set `_api_resource_class` and `_api_operation_name`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 exact 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 $book, BookPublishingHandler $bookPublishingHandler): Book
{
return $bookPublishingHandler->handle($book);
}
}
```

```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_operation_name: post_publication
```

## Defining Which Operation to Use to Generate the IRI

Using multiple operations on your resource, you may want to specify which operation to use to generate the IRI, instead
Expand Down