Core: [Question] Custom item operation action without identifier?

Created on 22 Aug 2016  路  2Comments  路  Source: api-platform/core

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
    }

}
question

All 2 comments

IMO, as it's not a regular REST route, you've to do a custom data provider where you intercept /auth/me ($id == 'me').

@soyuka Thanks! I created a custom data provider with a higher priority that intercepts me for the User resource.

Was this page helpful?
0 / 5 - 0 ratings