admin管理员组

文章数量:1302504

I am working with:

  • Docker version 27.5.1, build 9f9e405
  • Symfony CLI version 5.10.7
  • api-platform/symfony: 4.0.11
  • doctrine/mongodb-odm-bundle: 5.2.0
  • symfony/framework-bundle: 6.4.14

I'm just starting to setup a new app, gone through all the basic steps in the guides for both the API Framework and the MongoDB/API Framework and gotten to where I want to add my own Document and am now stuck with 404's from my API.

  • Symfony Docs
  • Mongo Docs

My Document type is listed on the domain/docs page, with all the basic CRUD function endpoints. However, when I attempt to access those endpoints, they all return 404s.

Part of my confusions is that, while I'm assured that my connection string is correct because I've used MongoDB and Symfony in another experiment for this project, I don't know if this version is having problems connecting to the database or if there is some other problem I'm not aware of. The API doesn't give a lot of clarity.

Here is my Document class in full:

<?php

declare(strict_types=1);

namespace App\Document;

use ApiPlatform\Metadata\ApiResource;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Doctrine\ODM\MongoDB\Mapping\Annotations\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations\Field;
use Doctrine\ODM\MongoDB\Mapping\Annotations\Id;
use App\Document\State;
use App\Document\Partner;

#[ApiResource]
#[ODM\Document(collection: 'reports')]
#[ODM\HasLifecycleCallbacks]
class Report
{
    #[ODM\Id]
    public ?string $id = null;

    #[ODM\Field(type: 'string')]
    public string $name;

    #[ODM\Field(type: 'string')]
    public string $date;

    #[ODM\Field(type: 'string')]
    public string $author;

    #[ODM\Field(type: 'string')]
    public string $report_type;

    #[ODM\Field(type: 'string')]
    public string $description;

    #[ODM\Field(type: 'string')]
    public string $dews_region;

    #[ODM\Field(type: 'string')]
    public string $climate_region;

    #[ODM\ReferenceMany(targetDocument: State::class)]
    #[ODM\Index(keys: ['name'])]

    public ArrayCollection $states;

    #[ODM\ReferenceMany(targetDocument: Partner::class)]
    public ArrayCollection $partners;

    #[ODM\EmbedMany(targetDocument: Section::class)]
    #[ODM\Index(keys: ['order'])]
    public ArrayCollection $sections;

    #[ODM\Field(type: 'date')]
    private \DateTime $createdAt;

    #[ODM\Field(type: 'date')]
    private \DateTime $updatedAt;

    public function __construct()
    {
        $this->sections = new ArrayCollection();
        $this->partners = new ArrayCollection();
        $this->states = new ArrayCollection();
    }

    public function getName(): string {
        return $this->name;
    }

    public function setName( string $name ): void {
        $this->name = $name;
    }

    public function getDate(): string {
        return $this->date;
    }

    public function setDate( string $date ): void {
        $this->date = $date;
    }

    public function getAuthor(): string {
        return $this->author;
    }

    public function setAuthor( string $author ): void {
        $this->author = $author;
    }

    public function getReportType(): string {
        return $this->report_type;
    }

    public function setReportType( string $report_type ): void {
        $this->report_type = $report_type;
    }

    public function getDescription(): string {
        return $this->description;
    }

    public function setDescription( string $description ): void {
        $this->description = $description;
    }

    public function getDewsRegion(): string {
        return $this->dews_region;
    }

    public function setDewsRegion( string $dews_region ): void {
        $this->dews_region = $dews_region;
    }

    public function getClimateRegion(): string {
        return $this->climate_region;
    }

    public function setClimateRegion( string $climate_region ): void {
        $this->climate_region = $climate_region;
    }

    public function getStates(): Collection {
        return $this->states;
    }

    public function addState(State $state): void { $this->states[] = $state; }

    public function getPartners(): Collection {
        return $this->partners;
    }

    public function addPartner(Partner $partner): void { $this->partners[] = $partner; }

    public function getSections(): Collection {
        return $this->sections;
    }

    public function addSection(Section $section): void { $this->sections[] = $section; }

    public function getCreatedAt(): \DateTime {
        return $this->createdAt;
    }

    public function getUpdatedAt(): \DateTime {
        return $this->updatedAt;
    }

    #[ODM\PrePersist]
    public function prePersist(): void {
        $this->createdAt = new \DateTime();
    }

    #[ODM\PreUpdate]
    public function preUpdate(): void {
        $this->updatedAt = new \DateTime();
    }
}

Additional details

Here is the curl request:

curl -X 'GET' \
  'https://localhost/reports?page=1' \
  -H 'accept: application/ld+json'

Here are the response headers:

alt-svc: h3=":443"; ma=2592000 
 cache-control: no-cache,private 
 content-location: /errors/404.jsonld?page=1 
 content-type: application/problem+json; charset=utf-8 
 date: Wed,12 Feb 2025 13:00:33 GMT 
 link: <;; rel=";,<https://localhost/docs.jsonld>; rel="; 
 permissions-policy: browsing-topics=() 
 server: Caddy 
 vary: Accept 
 x-content-type-options: nosniff 
 x-debug-token: 437fb7 
 x-debug-token-link: https://localhost/_profiler/437fb7 
 x-frame-options: deny 
 x-previous-debug-token: cc670c 
 x-robots-tag: noindex 

And here is the response text:

{
  "@context": "/contexts/Error",
  "@id": "/errors/404",
  "@type": "Error",
  "title": "An error occurred",
  "detail": "Not Found",
  "status": 404,
  "type": "/errors/404",
  "trace": [
    {
      "file": "/app/vendor/api-platform/symfony/Bundle/SwaggerUi/SwaggerUiProvider.php",
      "line": 50,
      "function": "provide",
      "class": "ApiPlatform\\State\\Provider\\ReadProvider",
      "type": "->"
    },
    {
      "file": "/app/vendor/api-platform/symfony/Security/State/AccessCheckerProvider.php",
      "line": 62,
      "function": "provide",
      "class": "ApiPlatform\\Symfony\\Bundle\\SwaggerUi\\SwaggerUiProvider",
      "type": "->"
    },
    {
      "file": "/app/vendor/api-platform/state/Provider/SecurityParameterProvider.php",
      "line": 39,
      "function": "provide",
      "class": "ApiPlatform\\Symfony\\Security\\State\\AccessCheckerProvider",
      "type": "->"
    },
    {
      "file": "/app/vendor/api-platform/state/Provider/DeserializeProvider.php",
      "line": 56,
      "function": "provide",
      "class": "ApiPlatform\\State\\Provider\\SecurityParameterProvider",
      "type": "->"
    },
    {
      "file": "/app/vendor/api-platform/symfony/Security/State/AccessCheckerProvider.php",
      "line": 62,
      "function": "provide",
      "class": "ApiPlatform\\State\\Provider\\DeserializeProvider",
      "type": "->"
    },
    {
      "file": "/app/vendor/api-platform/symfony/Validator/State/ValidateProvider.php",
      "line": 32,
      "function": "provide",
      "class": "ApiPlatform\\Symfony\\Security\\State\\AccessCheckerProvider",
      "type": "->"
    },
    {
      "file": "/app/vendor/api-platform/symfony/Security/State/AccessCheckerProvider.php",
      "line": 62,
      "function": "provide",
      "class": "ApiPlatform\\Symfony\\Validator\\State\\ValidateProvider",
      "type": "->"
    },
    {
      "file": "/app/vendor/api-platform/symfony/Validator/State/ParameterValidatorProvider.php",
      "line": 87,
      "function": "provide",
      "class": "ApiPlatform\\Symfony\\Security\\State\\AccessCheckerProvider",
      "type": "->"
    },
    {
      "file": "/app/vendor/api-platform/state/Provider/ParameterProvider.php",
      "line": 103,
      "function": "provide",
      "class": "ApiPlatform\\Symfony\\Validator\\State\\ParameterValidatorProvider",
      "type": "->"
    },
    {
      "file": "/app/vendor/api-platform/state/Provider/ContentNegotiationProvider.php",
      "line": 51,
      "function": "provide",
      "class": "ApiPlatform\\State\\Provider\\ParameterProvider",
      "type": "->"
    },
    {
      "file": "/app/vendor/api-platform/symfony/Controller/MainController.php",
      "line": 83,
      "function": "provide",
      "class": "ApiPlatform\\State\\Provider\\ContentNegotiationProvider",
      "type": "->"
    },
    {
      "file": "/app/vendor/symfony/http-kernel/HttpKernel.php",
      "line": 181,
      "function": "__invoke",
      "class": "ApiPlatform\\Symfony\\Controller\\MainController",
      "type": "->"
    },
    {
      "file": "/app/vendor/symfony/http-kernel/HttpKernel.php",
      "line": 76,
      "function": "handleRaw",
      "class": "Symfony\\Component\\HttpKernel\\HttpKernel",
      "type": "->"
    },
    {
      "file": "/app/vendor/symfony/http-kernel/Kernel.php",
      "line": 197,
      "function": "handle",
      "class": "Symfony\\Component\\HttpKernel\\HttpKernel",
      "type": "->"
    },
    {
      "file": "/app/vendor/symfony/runtime/Runner/Symfony/HttpKernelRunner.php",
      "line": 35,
      "function": "handle",
      "class": "Symfony\\Component\\HttpKernel\\Kernel",
      "type": "->"
    },
    {
      "file": "/app/vendor/autoload_runtime.php",
      "line": 29,
      "function": "run",
      "class": "Symfony\\Component\\Runtime\\Runner\\Symfony\\HttpKernelRunner",
      "type": "->"
    },
    {
      "file": "/app/public/index.php",
      "line": 5,
      "function": "require_once"
    }
  ],
  "description": "Not Found"
}

本文标签: mongodbMy Symfony API Platform Endpoints are returning 404sStack Overflow