api-platform / core

The server component of API Platform: hypermedia and GraphQL APIs in minutes
https://api-platform.com
MIT License
2.44k stars 868 forks source link

Autowiring Actions fail #1334

Closed Drachenkaetzchen closed 7 years ago

Drachenkaetzchen commented 7 years ago

Hi,

I'm upgrading my project to Symfony 3.3 and the new api-platform. However, when I attempt to autowire an action, I receive the error that no services are passed to my action. I have enabled service autowiring.

It is noteworthy that the action is instanciated by Symfonys ControllerResolver line 202, where it creates a new instance of my class without any parameters appended.

Do you have any idea where I could start looking?

Entity:

/**
 * @ORM\Entity
 * @ORM\Table(
 *      name="PartKeeprUser",
 *      uniqueConstraints={@ORM\UniqueConstraint(name="username_provider", columns={"username", "provider_id"})})
 * @TargetService(uri="/api/users")
 *
 * @ApiResource(
 *     collectionOperations={
 *          "user_preferences"={"route_name"="user_get_preferences"}
 *     },
 *     attributes={"filters"={"doctrine_reflection_service.search_filter"}}
 *  )
 */
class User extends BaseEntity implements UserInterface, EquatableInterface
{

routing.yml:

auth_actions:
    resource: "@PartKeeprAuthBundle/Action"
    type:     annotation

Action:

<?php

namespace PartKeepr\AuthBundle\Action;

use PartKeepr\AuthBundle\Services\UserPreferenceService;
use PartKeepr\AuthBundle\Services\UserService;
use PartKeepr\AuthBundle\Entity\User;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\Serializer;

/**
 * Returns the tree root node.
 */
class GetPreferencesAction
{
    /**
     * @var UserService
     */
    private $userService;

    /**
     * @var UserPreferenceService
     */
    private $userPreferenceService;

    /**
     * @var Serializer
     */
    private $serializer;

    public function __construct(
       /* UserService $userService,
        UserPreferenceService $userPreferenceService,*/
        Serializer $serializer
    ) {
        /*$this->userService = $userService;
        $this->userPreferenceService = $userPreferenceService;*/
        $this->serializer = $serializer;
    }

    /**
     * Retrieves a collection of resources.
     *
     * @Route(
     *     name="user_get_preferences",
     *     path="/api/user_preferences",
     *     defaults={"_api_resource_class"=User::class, "_api_collection_operation_name"="user_preferences"}
     * )
     * @Method("GET")
     * @param Request $request
     * @return JsonResponse
     */

    public function __invoke($data, Request $request)
    {
        die("FOO");
        $user = $this->userService->getUser();

        $preferences = $this->userPreferenceService->getPreferences($user);

        /*
         * @var ResourceInterface $resourceType
         */
        $serializedData = $this->serializer->normalize(
            $preferences,
            'json'
        );

        return new JsonResponse($serializedData);
    }
}
OnekO commented 7 years ago

Same problem here with a very similar configuration. Any advice?

OnekO commented 7 years ago

Solved thanks to this: https://github.com/api-platform/api-platform/issues/317#issuecomment-316436505

Just add something like

AppBundle\Action\:
        resource: '../../src/AppBundle/Action'
        public: true
        autowire: true
        tags: ['controller.service_arguments']

to your services.yml

dunglas commented 7 years ago

Fixed in API Platform 2.1.