Skip to content

Commit 1081cb4

Browse files
authored
docs: use attributes in controllers page (#1389)
1 parent e75c656 commit 1081cb4

File tree

4 files changed

+59
-51
lines changed

4 files changed

+59
-51
lines changed

core/controllers.md

Lines changed: 47 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@ First, let's create your custom operation:
3535
namespace App\Controller;
3636

3737
use App\Entity\Book;
38+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
39+
use Symfony\Component\HttpKernel\Attribute\AsController;
3840

39-
class CreateBookPublication
41+
#[AsController]
42+
class CreateBookPublication extends AbstractController
4043
{
4144
private $bookPublishingHandler;
4245

@@ -98,7 +101,7 @@ use App\Controller\CreateBookPublication;
98101
])]
99102
class Book
100103
{
101-
//...
104+
// ...
102105
}
103106
```
104107

@@ -154,24 +157,20 @@ use ApiPlatform\Core\Annotation\ApiResource;
154157
use App\Controller\CreateBookPublication;
155158
use Symfony\Component\Serializer\Annotation\Groups;
156159

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+
])]
168169
class Book
169170
{
170-
//...
171+
// ...
171172

172-
/**
173-
* @Groups("publication")
174-
*/
173+
#[Groups(['publication'])]
175174
public $isbn;
176175

177176
// ...
@@ -233,20 +232,18 @@ operation attribute:
233232
use ApiPlatform\Core\Annotation\ApiResource;
234233
use App\Controller\CreateBookPublication;
235234

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+
])]
247244
class Book
248245
{
249-
//...
246+
// ...
250247
}
251248
```
252249

@@ -309,16 +306,14 @@ First, let's create your resource configuration:
309306

310307
use ApiPlatform\Core\Annotation\ApiResource;
311308

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+
])]
319314
class Book
320315
{
321-
//...
316+
// ...
322317
}
323318
```
324319

@@ -364,9 +359,12 @@ and its related route using annotations:
364359
namespace App\Controller;
365360

366361
use App\Entity\Book;
362+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
363+
use Symfony\Component\HttpKernel\Attribute\AsController;
367364
use Symfony\Component\Routing\Annotation\Route;
368365

369-
class CreateBookPublication
366+
#[AsController]
367+
class CreateBookPublication extends AbstractController
370368
{
371369
private $bookPublishingHandler;
372370

@@ -375,17 +373,15 @@ class CreateBookPublication
375373
$this->bookPublishingHandler = $bookPublishingHandler;
376374
}
377375

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+
)]
389385
public function __invoke(Book $data): Book
390386
{
391387
$this->bookPublishingHandler->handle($data);
@@ -409,7 +405,9 @@ namespace App\Controller;
409405

410406
use App\Entity\Book;
411407
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
408+
use Symfony\Component\HttpKernel\Attribute\AsController;
412409

410+
#[AsController]
413411
class BookController extends AbstractController
414412
{
415413
public function createPublication(Book $data, BookPublishingHandler $bookPublishingHandler): Book

core/file-upload.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,13 @@ that handles the file upload.
137137
namespace App\Controller;
138138
139139
use App\Entity\MediaObject;
140+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
140141
use Symfony\Component\HttpFoundation\Request;
142+
use Symfony\Component\HttpKernel\Attribute\AsController;
141143
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
142144
143-
final class CreateMediaObjectAction
145+
#[AsController]
146+
final class CreateMediaObjectAction extends AbstractController
144147
{
145148
public function __invoke(Request $request): MediaObject
146149
{

core/operations.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,9 +415,12 @@ and its related route using annotations:
415415
namespace App\Controller;
416416

417417
use App\Entity\Book;
418+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
419+
use Symfony\Component\HttpKernel\Attribute\AsController;
418420
use Symfony\Component\Routing\Annotation\Route;
419421

420-
class CreateBookPublication
422+
#[AsController]
423+
class CreateBookPublication extends AbstractController
421424
{
422425
public function __construct(
423426
private BookPublishingHandler $bookPublishingHandler
@@ -455,7 +458,9 @@ namespace App\Controller;
455458

456459
use App\Entity\Book;
457460
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
461+
use Symfony\Component\HttpKernel\Attribute\AsController;
458462

463+
#[AsController]
459464
class BookController extends AbstractController
460465
{
461466
public function createPublication(Book $data, BookPublishingHandler $bookPublishingHandler): Book

core/pagination.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,8 +492,10 @@ namespace App\Controller\Book;
492492
use ApiPlatform\Core\Bridge\Doctrine\Orm\Paginator;
493493
use App\Repository\BookRepository;
494494
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
495+
use Symfony\Component\HttpKernel\Attribute\AsController;
495496
use Symfony\Component\HttpFoundation\Request;
496497
498+
#[AsController]
497499
class GetBooksByFavoriteAuthorAction extends AbstractController
498500
{
499501
public function __invoke(Request $request, BookRepository $bookRepository): Paginator

0 commit comments

Comments
 (0)