Skip to content

fix: Clarify __invoke distinction #1358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 23, 2021
Merged

Conversation

botris
Copy link
Contributor

@botris botris commented May 10, 2021

When creating custom controllers the invoke method parameter name differs depending on the HTTP method.

The invoke method parameter differs from POST to GET methods.
@botris botris changed the title Clarify __invoke distinction fix: Clarify __invoke distinction May 10, 2021
@alanpoulain
Copy link
Member

Are you sure it's the case? Where is the attribute set?

botris and others added 2 commits May 10, 2021 15:33
add comma for readability

Co-authored-by: Alan Poulain <[email protected]>
Language update

Co-authored-by: Alan Poulain <[email protected]>
@botris
Copy link
Contributor Author

botris commented May 12, 2021

When I have this entity:

/**
 * @ApiResource(
 *     itemOperations={
 *         "example_start": {
 *              "method": "GET",
 *              "path": "/example/start/{uuid}.{_format}",
 *              "controller": StartExampleController::class,
 *              "read": false,
 *          }
 *     },
 *     collectionOperations={}
 * )
 */
class Example
{
    /**
     * @ApiProperty(identifier=true)
     */
    private UuidInterface $uuid;

And I do this in my controller

public function __invoke($data): Response
{
//
}

I get: "Could not resolve argument $data of \"app\\controller\\startexamplecontroller()\", maybe you forgot to register the controller as a service or missed tagging it with the \"controller.service_arguments\"?"

But it works if I do

public function __invoke($uuid): Response
{
//
}

Which leads me to believe that the argument name must match the identifier name

@alanpoulain
Copy link
Member

alanpoulain commented Jun 23, 2021

OK I see why now, it's because the route parameters are given directly to the controller method.
I've also added your change there #1390, thank you!

@alanpoulain alanpoulain reopened this Jun 23, 2021
@alanpoulain alanpoulain merged commit 6608635 into api-platform:2.6 Jun 23, 2021
@@ -65,7 +65,8 @@ This action will be automatically registered as a service (the service name is t
API Platform automatically retrieves the appropriate PHP entity using the data provider then deserializes user data in it,
and for `POST` and `PUT` requests updates the entity with data provided by the user.

**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!
**Warning: when using `POST` or `PUT`, 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!
When using `GET`, the `__invoke()` method parameter should be called the same as the entity identifier. So for the path `/user/{uuid}/bookmarks`, you must use `__invoke($uuid)`.
Copy link

@guilliamxavier guilliamxavier Jun 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's wrong: the cause was not (only) you using GET but "read": false

(I have used GET with $data, working)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also retrieve the identifier even if read is true. But you are right, it should be modified to say that is also works for GET.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See: #1392

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants