diff --git a/core/controllers.md b/core/controllers.md index 9b90f1304b8..6d54f2d515a 100644 --- a/core/controllers.md +++ b/core/controllers.md @@ -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', diff --git a/core/operations.md b/core/operations.md index 25dc7bdb65e..6ffa270af67 100644 --- a/core/operations.md +++ b/core/operations.md @@ -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 @@ -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"> - - - - book_post_publication - - - + /library ``` @@ -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 - 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 -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