【发布时间】:2022-12-28 04:05:18
【问题描述】:
My controller doesnt work on GET method but it works on PUT. I dont know why. If anybody have an answer. I know that in v2.6 there is a difference between operationitems and collectionitems but in v3 there is only operation. I think it must be a syntax problem but I don't know. I'm asking you the question because I've been looking for the solution for three days and I'm starting to have a headache. It returns me a 404 error. It does not find the path to the controller.
My entity
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
{
My countcontroller
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;
}
}
And my working publishcontroller
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;
}
}
The ApiPlatform response : { "@context": "/api/contexts/Error", "@type": "hydra:Error", "hydra:title": "An error occurred", "hydra:description": "Not Found", "trace": [ { "namespace": "", "short_class": "", "class": "", "type": "", "function": "", "file": "C:\Users\God\Desktop\Api\api2\vendor\api-platform\core\src\Symfony\EventListener\ReadListener.php", "line": 94, "args": [] },
Thank you!
【问题讨论】:
标签: php symfony api-platform.com