自定义控制器Api平台的问题

问题描述 投票:0回答:0

我的控制器不适用于 GET 方法,但适用于 PUT。我不知道为什么。如果有人有答案的话。 我知道在 v2.6 中,操作项和集合项之间存在差异,但在 v3 中只有操作。我认为这一定是语法问题,但我不知道。 我问你这个问题是因为我已经寻找解决方案三天了,但我开始头疼了。 它返回给我一个 404 错误。它找不到控制器的路径。

我的实体

    namespace App\Entity;

use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use App\Controller\CommentaireCountController;
use App\Controller\CommentairePublishController;
use App\Repository\CommentaireRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Metadata\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;


#[ORM\Entity(repositoryClass: CommentaireRepository::class)]
#[ApiResource(
    operations: [
        new Post(
            openapiContext: [
                'summary' => 'poste ton commentaire'
            ],
            validationContext: ['groups' => ['create:commentaire']]
        ),
        new Get(
            normalizationContext: ['groups'=>['read:blabla']]
        ),
        new Put(
            openapiContext: [
                'summary' => 'Remplace un champ de commentaire'
            ],
            denormalizationContext: ['groups'=>['write:commentaire']],
            validationContext: ['groups' => ['create:commentaire']] 
        ),
        new Delete(),
        new Patch(),
    ],
)]
#[Put(
    uriTemplate: '/commentaires/{id}/publish',
    controller: CommentairePublishController::class,
    openapiContext: [
        'summary' => 'Mettre en ligne un commentaire',
        'description' => 'Mettre en ligne le commentaire'
    ],
    normalizationContext: ['groups'=>['read:commentaire']],
    denormalizationContext: ['groups'=>['publish:commentaire']],
    name: 'Publish'
)]
#[Get(
    uriTemplate: '/commentaires/count',
    controller: CommentaireCountController::class,
    name: 'Count'
)]
#[ApiFilter(SearchFilter::class, properties: ['id' => 'exact', 'proprietaire.name' => 'iexact'])]
class Commentaire
    {

我的计数控制器

    namespace App\Controller;

use App\Entity\Commentaire;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\Routing\Annotation\Route;

#[AsController]
class CommentaireCountController
{
    public function __invoke(): int
    {
        return 10;
    }
    }

还有我正在工作的发布控制器

    namespace App\Controller;
use App\Entity\Commentaire;
use Symfony\Component\HttpKernel\Attribute\AsController;

#[AsController]
class CommentairePublishController
{
    public function __invoke(Commentaire $data): Commentaire{
    $data->setOnline(true);
    return $data;
}
    }

Api平台响应: { "@context": "/api/contexts/Error", "@type": "九头蛇:错误", "Hydra:title": "发生错误", "Hydra:description": "未找到", “痕迹”: [ { “命名空间”:“”, “短类”:“”, “班级”: ””, “类型”: ””, “功能”: ””, "file": "C:\Users\God\Desktop\Api pi2 或 pi-platfor

php symfony api-platform.com
© www.soinside.com 2019 - 2024. All rights reserved.