Api Plateforme V3 : Custom controller - 404 Not found

38 views Asked by At

I'm testing API Platform V3 with Symfony 7 and I'm trying to call an endpoint using curl. Here's the command I'm using:



curl

 -X 'GET' \
  'http://localhost/api/posts/count' \
  -H 'accept: application/ld+json'

However, I'm receiving a 404 error with the following message:

{
  "@id": "/api/errors/404",
  "@type": "hydra:Error",
  "title": "An error occurred",
  "detail": "Not Found",
  "status": 404,
  "type": "/errors/404",
  "trace": [
    {
      "file": "/var/www/html/vendor/api-platform/core/src/Symfony/Bundle/SwaggerUi/SwaggerUiProvider.php",
      "line": 48,
      "function": "provide",
      "class": "ApiPlatform\\State\\Provider\\ReadProvider",
      "type": "->"
    },
  ...
}

Here's an excerpt from the Post.php file where I define the API:

use ArrayObject;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints\Valid;

#[ORM\Entity(repositoryClass: PostRepository::class)]
#[
    ApiResource(
        normalizationContext: [
            'groups' => ['read:Post'],
            'openapi_definition_name' => 'Post'
        ],
        denormalizationContext: ['groups' => ['write:Post']],
        paginationItemsPerPage: 2,
        operations: [
            new Get(),
            new GetCollection(),
            new MetadataPost(),
            new Put(),
            new Patch(),
            new GetCollection(
                name: 'count',
                uriTemplate: '/posts/count',
                controller: PostCountController::class,
                read: false,
                filters: [],
                paginationEnabled: false,
                openapi: new Operation(
                    summary: "Returns the number of articles",
                    parameters: [ ]
                )
            )
...

I don't understand why I'm receiving this error. Could someone help me understand what's going wrong?

Thank you in advance for your help.


I tried to call the /posts/count endpoint of my API using the curl command:

I expected to receive a response with the count of posts in my database. However, I received a 404 error with the message "Not Found". This is unexpected as I have defined the GetCollection operation for the /posts/count endpoint in my Post.php file. I'm not sure why the endpoint is not found.

0

There are 0 answers