Skip to content

Commit 7a0f19f

Browse files
authored
fix: remove duplicated docs and fix constructors (api-platform#1702)
1 parent 45dc5d1 commit 7a0f19f

File tree

2 files changed

+3
-81
lines changed

2 files changed

+3
-81
lines changed

core/controllers.md

+3-6
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,9 @@ use Symfony\Component\HttpKernel\Attribute\AsController;
4444
#[AsController]
4545
class CreateBookPublication extends AbstractController
4646
{
47-
private $bookPublishingHandler;
48-
49-
public function __construct(BookPublishingHandler $bookPublishingHandler)
50-
{
51-
$this->bookPublishingHandler = $bookPublishingHandler;
52-
}
47+
public function __construct(
48+
private BookPublishingHandler $bookPublishingHandler
49+
) {}
5350

5451
public function __invoke(Book $book): Book
5552
{

core/operations.md

-75
Original file line numberDiff line numberDiff line change
@@ -367,81 +367,6 @@ App\Entity\Book:
367367

368368
[/codeSelector]
369369

370-
API Platform will automatically map this `post_publication` operation to the route `book_post_publication`. Let's create a custom action
371-
and its related route using annotations:
372-
373-
```php
374-
<?php
375-
// api/src/Controller/CreateBookPublication.php
376-
377-
namespace App\Controller;
378-
379-
use App\Entity\Book;
380-
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
381-
use Symfony\Component\HttpKernel\Attribute\AsController;
382-
use Symfony\Component\Routing\Annotation\Route;
383-
384-
#[AsController]
385-
class CreateBookPublication extends AbstractController
386-
{
387-
public function __construct(
388-
private BookPublishingHandler $bookPublishingHandler
389-
) {}
390-
391-
#[Route(
392-
path: '/books/{id}/publication',
393-
name: 'book_post_publication',
394-
defaults: [
395-
'_api_resource_class' => Book::class,
396-
'_api_operation_name' => '_api_/books/{id}/publication_post',
397-
],
398-
methods: ['POST'],
399-
)]
400-
public function __invoke(Book $book): Book
401-
{
402-
$this->bookPublishingHandler->handle($book);
403-
404-
return $book;
405-
}
406-
}
407-
```
408-
409-
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.
410-
411-
Alternatively, you can also use a traditional Symfony controller and YAML or XML route declarations. The following example does
412-
the exact same thing as the previous example:
413-
414-
```php
415-
<?php
416-
// api/src/Controller/BookController.php
417-
418-
namespace App\Controller;
419-
420-
use App\Entity\Book;
421-
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
422-
use Symfony\Component\HttpKernel\Attribute\AsController;
423-
424-
#[AsController]
425-
class BookController extends AbstractController
426-
{
427-
public function createPublication(Book $book, BookPublishingHandler $bookPublishingHandler): Book
428-
{
429-
return $bookPublishingHandler->handle($book);
430-
}
431-
}
432-
```
433-
434-
```yaml
435-
# api/config/routes.yaml
436-
book_post_publication:
437-
path: /books/{id}/publication
438-
methods: ['POST']
439-
defaults:
440-
_controller: App\Controller\BookController::createPublication
441-
_api_resource_class: App\Entity\Book
442-
_api_operation_name: post_publication
443-
```
444-
445370
## Defining Which Operation to Use to Generate the IRI
446371

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

0 commit comments

Comments
 (0)