Skip to content

Commit 3b0828e

Browse files
authored
feat: thanks to the argument resolver, no need to use $data anymore (#1390)
1 parent 07077a8 commit 3b0828e

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

core/controllers.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ class CreateBookPublication
4545
$this->bookPublishingHandler = $bookPublishingHandler;
4646
}
4747

48-
public function __invoke(Book $data): Book
48+
public function __invoke(Book $book): Book
4949
{
50-
$this->bookPublishingHandler->handle($data);
50+
$this->bookPublishingHandler->handle($book);
5151

52-
return $data;
52+
return $book;
5353
}
5454
}
5555
```
@@ -65,7 +65,10 @@ This action will be automatically registered as a service (the service name is t
6565
API Platform automatically retrieves the appropriate PHP entity using the data provider then deserializes user data in it,
6666
and for `POST` and `PUT` requests updates the entity with data provided by the user.
6767

68-
**Warning: the `__invoke()` method parameter [MUST be called `$data`](https://symfony.com/doc/current/components/http_kernel.html#getting-the-controller-arguments)**, otherwise, it will not be filled correctly!
68+
The entity is retrieved in the `__invoke` method thanks to a dedicated argument resolver.
69+
70+
When using `GET`, the `__invoke()` method parameter will receive the identifier and should be called the same as the resource identifier.
71+
So for the path `/user/{uuid}/bookmarks`, you must use `__invoke(string $uuid)`.
6972

7073
Services (`$bookPublishingHandler` here) are automatically injected thanks to the autowiring feature. You can type-hint any service
7174
you need and it will be autowired too.
@@ -386,11 +389,11 @@ class CreateBookPublication
386389
* }
387390
* )
388391
*/
389-
public function __invoke(Book $data): Book
392+
public function __invoke(Book $book): Book
390393
{
391-
$this->bookPublishingHandler->handle($data);
394+
$this->bookPublishingHandler->handle($book);
392395

393-
return $data;
396+
return $book;
394397
}
395398
}
396399
```
@@ -412,9 +415,9 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
412415

413416
class BookController extends AbstractController
414417
{
415-
public function createPublication(Book $data, BookPublishingHandler $bookPublishingHandler): Book
418+
public function createPublication(Book $book, BookPublishingHandler $bookPublishingHandler): Book
416419
{
417-
return $bookPublishingHandler->handle($data);
420+
return $bookPublishingHandler->handle($book);
418421
}
419422
}
420423
```

core/operations.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -432,11 +432,11 @@ class CreateBookPublication
432432
],
433433
methods: ['POST'],
434434
)]
435-
public function __invoke(Book $data): Book
435+
public function __invoke(Book $book): Book
436436
{
437-
$this->bookPublishingHandler->handle($data);
437+
$this->bookPublishingHandler->handle($book);
438438

439-
return $data;
439+
return $book;
440440
}
441441
}
442442
```
@@ -458,9 +458,9 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
458458

459459
class BookController extends AbstractController
460460
{
461-
public function createPublication(Book $data, BookPublishingHandler $bookPublishingHandler): Book
461+
public function createPublication(Book $book, BookPublishingHandler $bookPublishingHandler): Book
462462
{
463-
return $bookPublishingHandler->handle($data);
463+
return $bookPublishingHandler->handle($book);
464464
}
465465
}
466466
```

0 commit comments

Comments
 (0)