@@ -35,8 +35,11 @@ First, let's create your custom operation:
35
35
namespace App\Controller;
36
36
37
37
use App\Entity\Book;
38
+ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
39
+ use Symfony\Component\HttpKernel\Attribute\AsController;
38
40
39
- class CreateBookPublication
41
+ #[AsController]
42
+ class CreateBookPublication extends AbstractController
40
43
{
41
44
private $bookPublishingHandler;
42
45
@@ -98,7 +101,7 @@ use App\Controller\CreateBookPublication;
98
101
])]
99
102
class Book
100
103
{
101
- //...
104
+ // ...
102
105
}
103
106
```
104
107
@@ -154,24 +157,20 @@ use ApiPlatform\Core\Annotation\ApiResource;
154
157
use App\Controller\CreateBookPublication;
155
158
use Symfony\Component\Serializer\Annotation\Groups;
156
159
157
- /**
158
- * @ApiResource(itemOperations={
159
- * "get",
160
- * "post_publication"={
161
- * "method"="POST",
162
- * "path"="/books/{id}/publication",
163
- * "controller"=CreateBookPublication::class,
164
- * "normalization_context"={"groups"={"publication"}},
165
- * }
166
- * })
167
- */
160
+ #[ApiResource(itemOperations: [
161
+ 'get',
162
+ 'post_publication' => [
163
+ 'method' => 'POST',
164
+ 'path' => '/books/{id}/publication',
165
+ 'controller' => CreateBookPublication::class,
166
+ 'normalization_context' => ['groups' => 'publication'],
167
+ ],
168
+ ])]
168
169
class Book
169
170
{
170
- //...
171
+ // ...
171
172
172
- /**
173
- * @Groups("publication")
174
- */
173
+ #[Groups(['publication'])]
175
174
public $isbn;
176
175
177
176
// ...
@@ -233,20 +232,18 @@ operation attribute:
233
232
use ApiPlatform\Core\Annotation\ApiResource;
234
233
use App\Controller\CreateBookPublication;
235
234
236
- /**
237
- * @ApiResource(itemOperations={
238
- * "get",
239
- * "post_publication"={
240
- * "method"="POST",
241
- * "path"="/books/{id}/publication",
242
- * "controller"=CreateBookPublication::class,
243
- * "read"=false,
244
- * }
245
- * })
246
- */
235
+ #[ApiResource(itemOperations: [
236
+ 'get',
237
+ 'post_publication' => [
238
+ 'method' => 'POST',
239
+ 'path' => '/books/{id}/publication',
240
+ 'controller' => CreateBookPublication::class,
241
+ 'read' => false,
242
+ ],
243
+ ])]
247
244
class Book
248
245
{
249
- //...
246
+ // ...
250
247
}
251
248
```
252
249
@@ -309,16 +306,14 @@ First, let's create your resource configuration:
309
306
310
307
use ApiPlatform\Core\Annotation\ApiResource;
311
308
312
- /**
313
- * @ApiResource(itemOperations={
314
- * "get",
315
- * "post_publication"={"route_name"="book_post_publication"},
316
- * "book_post_discontinuation",
317
- * })
318
- */
309
+ #[ApiResource(itemOperations: [
310
+ 'get',
311
+ 'post_publication' => ['route_name' => 'book_post_publication'],
312
+ 'book_post_discontinuation',
313
+ ])]
319
314
class Book
320
315
{
321
- //...
316
+ // ...
322
317
}
323
318
```
324
319
@@ -364,9 +359,12 @@ and its related route using annotations:
364
359
namespace App\Controller;
365
360
366
361
use App\Entity\Book;
362
+ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
363
+ use Symfony\Component\HttpKernel\Attribute\AsController;
367
364
use Symfony\Component\Routing\Annotation\Route;
368
365
369
- class CreateBookPublication
366
+ #[AsController]
367
+ class CreateBookPublication extends AbstractController
370
368
{
371
369
private $bookPublishingHandler;
372
370
@@ -375,17 +373,15 @@ class CreateBookPublication
375
373
$this->bookPublishingHandler = $bookPublishingHandler;
376
374
}
377
375
378
- /**
379
- * @Route(
380
- * name="book_post_publication",
381
- * path="/books/{id}/publication",
382
- * methods={"POST"},
383
- * defaults={
384
- * "_api_resource_class"=Book::class,
385
- * "_api_item_operation_name"="post_publication"
386
- * }
387
- * )
388
- */
376
+ #[Route(
377
+ name: 'book_post_publication',
378
+ path: '/books/{id}/publication',
379
+ methods: ['POST'],
380
+ defaults: [
381
+ '_api_resource_class' => Book::class,
382
+ '_api_item_operation_name' => 'post_publication',
383
+ ],
384
+ )]
389
385
public function __invoke(Book $data): Book
390
386
{
391
387
$this->bookPublishingHandler->handle($data);
@@ -409,7 +405,9 @@ namespace App\Controller;
409
405
410
406
use App\Entity\Book;
411
407
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
408
+ use Symfony\Component\HttpKernel\Attribute\AsController;
412
409
410
+ #[AsController]
413
411
class BookController extends AbstractController
414
412
{
415
413
public function createPublication(Book $data, BookPublishingHandler $bookPublishingHandler): Book
0 commit comments