@@ -379,11 +379,6 @@ class Book
379
379
App\Entity\Book :
380
380
attributes :
381
381
route_prefix : /library
382
- itemOperations :
383
- get : ~
384
- post_publication :
385
- route_name : book_post_publication
386
- book_post_discontinuation : ~
387
382
` ` `
388
383
389
384
` ` ` xml
@@ -395,13 +390,7 @@ App\Entity\Book:
395
390
xsi:schemaLocation="https://api-platform.com/schema/metadata
396
391
https://api-platform.com/schema/metadata/metadata-2.0.xsd">
397
392
<resource class="App\Entity\Book">
398
- <itemOperations>
399
- <itemOperation name="get" />
400
- <itemOperation name="post_publication">
401
- <attribute name="route_name">book_post_publication</attribute>
402
- </itemOperation>
403
- <itemOperation name="book_post_discontinuation" />
404
- </itemOperations>
393
+ <attribute name="route_prefix">/library</attribute>
405
394
</resource>
406
395
</resources>
407
396
```
@@ -410,83 +399,7 @@ App\Entity\Book:
410
399
411
400
Alternatively, the more verbose attribute syntax can be used: ` @ApiResource(attributes={"route_prefix"="/library"}) ` .
412
401
413
- API Platform will automatically map this ` post_publication ` operation to the route ` book_post_publication ` . Let's create a custom action
414
- and its related route using annotations:
415
-
416
- ``` php
417
- <?php
418
- // api/src/Controller/CreateBookPublication.php
419
-
420
- namespace App\Controller;
421
-
422
- use App\Entity\Book;
423
- use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
424
- use Symfony\Component\HttpKernel\Attribute\AsController;
425
- use Symfony\Component\Routing\Annotation\Route;
426
-
427
- #[AsController]
428
- class CreateBookPublication extends AbstractController
429
- {
430
- public function __construct(
431
- private BookPublishingHandler $bookPublishingHandler
432
- ) {}
433
-
434
- #[Route(
435
- path: '/books/{id}/publication',
436
- name: 'book_post_publication',
437
- defaults: [
438
- '_api_resource_class' => Book::class,
439
- '_api_item_operation_name' => 'post_publication',
440
- ],
441
- methods: ['POST'],
442
- )]
443
- public function __invoke(Book $data): Book
444
- {
445
- $this->bookPublishingHandler->handle($data);
446
-
447
- return $data;
448
- }
449
- }
450
- ```
451
-
452
- It is mandatory to set ` _api_resource_class ` and ` _api_item_operation_name ` (or ` _api_collection_operation_name ` for a collection
453
- operation) in the parameters of the route (` defaults ` key). It allows API Platform to work with the Symfony routing system.
454
-
455
- Alternatively, you can also use a traditional Symfony controller and YAML or XML route declarations. The following example does
456
- the same thing as the previous example:
457
-
458
- ``` php
459
- <?php
460
- // api/src/Controller/BookController.php
461
-
462
- namespace App\Controller;
463
-
464
- use App\Entity\Book;
465
- use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
466
- use Symfony\Component\HttpKernel\Attribute\AsController;
467
-
468
- #[AsController]
469
- class BookController extends AbstractController
470
- {
471
- public function createPublication(Book $data, BookPublishingHandler $bookPublishingHandler): Book
472
- {
473
- return $bookPublishingHandler->handle($data);
474
- }
475
- }
476
- ```
477
-
478
- ``` yaml
479
- # api/config/routes.yaml
480
- book_post_publication :
481
- path : /books/{id}/publication
482
- methods : ['POST']
483
- defaults :
484
- _controller : App\Controller\BookController::createPublication
485
- _api_resource_class : App\Entity\Book
486
- _api_item_operation_name : post_publication
487
- ` ` `
488
-
489
- ## Expose a model without any routes
402
+ ## Expose a Model Without Any Routes
490
403
491
404
Sometimes, you may want to expose a model, but want it to be used through subrequests only, and never through item or collection operations.
492
405
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
0 commit comments