Skip to content

[Question] Custom item operation action without identifier? #684

@bwegrzyn

Description

@bwegrzyn

I have a requirement that a certain API endpoint returns the currently authenticated user, so I'm trying to add a custom operation action to my User entity under the route /auth/me.

If I add the @route() annotation to my action with the path /auth/me, api-platform complains that the identifier is invalid.

If I add the @route() annotation to my action with the path /auth/{id} and try to intercept the ID when it is me, api-platform still complains because it cannot find a User with the ID of me before it runs the action.

How do I add a custom operation action where I can use an injected service to return an instance of User? In this case I would be using the symfony security component and returning the currently authenticated user.

namespace AppBundle\Action;

use AppBundle\Entity\User;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

class AuthenticatedUser
{

    /**
     * @Route(
     *     name="api_users_get_authenticated",
     *     path="/auth/me",
     *     defaults={"_api_resource_class"=User::class, "_api_item_operation_name"="get-authenticated"}
     * )
     * @Method("GET")
     */
    public function __invoke($data)
    {
        // Get currently authenticated user from symfony security component here
    }

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions