Lorenzo Piersante
Lorenzo Piersante

Reputation: 158

Symfony and Doctrine ParamConverter used out of an AbstractController

I'm trying to write a controller that doesn't extend Symfony's "classic" AbstractController and I'd like to use the ParamConverter inside as an annotation. The controller code is as follows:

<?php

declare(strict_types=1);

namespace App\Controller\Chat;

use App\Entity\Message;
use App\Entity\User;
use App\Service\DeleteMessage;
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Security;
use function assert;

final class MessageMarkAsDeletedController
{
    private DeleteMessage $deleteMessage;

    private Security $security;

    public function __construct(DeleteMessage $deleteMessage, Security $security)
    {
        $this->deleteMessage = $deleteMessage;
        $this->security      = $security;
    }

    /**
     * @Route("/message/{messageId}/remove", name="chat_message_mark_as_deleted", methods={"POST"})
     * @ParamConverter("message", class="App\Entity\Message", options={"id" = "messageId"})
     */
    public function markAsDeleted(Message $message) : JsonResponse
    {
        $user = $this->security->getUser();
        assert($user instanceof User);

        if (! ($message->getFromUser() === $user->getId())) {
            return new JsonResponse(['error' => 'You don\'t have permissions to delete this message'], 404);
        }

        if ($message->isDeleted()) {
            return new JsonResponse(['error' => 'The specified message has already been deleted'], 400);
        }

        $this->deleteMessage->delete($message);

        return new JsonResponse(['payload' => ['success' => true]], 204);
    }
}

When I push the code to gitlab, a pipeline starts which gives me the error:

Symfony \ Component \ Config \ Exception \ LoaderLoadException: [Semantical Error] The annotation "@Sensio \ Bundle \ FrameworkExtraBundle \ Request \ ParamConverter" in method App \ Controller \ Chat \ MessageMarkAsDeletedController :: markAsDeleted () does not exist, or could not be auto-loaded in /app/config/routes/../../src/Controller/Chat (which is being imported from "/app/config/routes/api.yaml"). Make sure annotations are installed and enabled.

Do you have any idea what the problem is? I have some problems understanding how to use the ParamConverter and on the symfony documentation all the examples are done in a controller class that extends AbstractController and I think my understanding problem comes from there

Upvotes: 1

Views: 2859

Answers (2)

Jakumi
Jakumi

Reputation: 8374

instead of

use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter;

(which IMHO does not exist) you should use

use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;

as suggested at https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html

Upvotes: 1

Martin Marx
Martin Marx

Reputation: 60

If it works locally but not with your pipeline (which I guess runs in a prod environment), it could mean that the ParamConverter is only available in a dev environment.

Please make sure that the sensio/framework-extra-bundle has not been registered as a require-dev dependency in your composer.json, but as a regulare require dependency.

To be sure, you can try to reinstall the bundle:

composer remove sensio/framework-extra-bundle && composer require sensio/framework-extra-bundle

Upvotes: 1

Related Questions