当我使用“php bin/console debug:router”命令时,我得到以下结果:
名称方法方案主机路径
_preview_error 任何任何任何 /_error/{code}.{_format}
这意味着我的路线没有被发现。我希望 / 和 /add-sample 路由在那里。
我在访问 http://127.0.0.1:8000/api/parties
上的端点时收到 404 错误Symfony\Component\Routing\Exception\ ResourceNotFoundException
No routes found for "/api/parties/".
我的路线未被识别的原因可能是什么?
Routes.yaml:
app_party:
resource: App\Controller\PartyController
type: annotation
PartyController.php:
<?php
namespace App\Controller;
use App\Entity\Party;
use App\Repository\PartyRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/api/parties", name="app_party_api_parties")
*/
class PartyController
{
/**
* @Route("/", name="api_party_list", methods={"GET"})
*/
public function list(PartyRepository $partyRepository): JsonResponse
{
$parties = $partyRepository->findAll();
$data = [];
foreach ($parties as $party) {
$data[] = [
'id' => $party->getId(),
'name' => $party->getName(),
'ideology' => $party->getIdeology(),
// Add more properties as needed
];
}
return new JsonResponse($data);
}
/**
* @Route("/add-sample", name="api_party_add_sample", methods={"POST"})
*/
public function addSampleParty(EntityManagerInterface $entityManager): JsonResponse
{
$party = new Party();
$party->setName('Sample Party');
$party->setIdeology('Some Ideology');
$party->setLeader('Sample Leader');
$entityManager->persist($party);
$entityManager->flush();
return new JsonResponse(['message' => 'Sample party added successfully.']);
}
}
我通过运行
composer require annotations
并更新 PartyController.php
: 解决了这个问题
<?php
namespace App\Controller;
use App\Entity\Party;
use App\Repository\PartyRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
#[Route('/api/parties', name: 'api_parties')]
class PartyController extends AbstractController
{
private EntityManagerInterface $entityManager;
private PartyRepository $partyRepository;
public function __construct(EntityManagerInterface $entityManager, PartyRepository $partyRepository)
{
$this->entityManager = $entityManager;
$this->partyRepository = $partyRepository;
}
#[Route('/', name: 'api_party_list', methods: ['GET'])]
public function list(): JsonResponse
{
$parties = $this->partyRepository->findAll();
$data = array_map(
fn(Party $party) => [
'id' => $party->getId(),
'name' => $party->getName(),
'ideology' => $party->getIdeology(),
],
$parties
);
return $this->json($data);
}
#[Route('/add-sample', name: 'api_party_add_sample', methods: ['POST'])]
public function addSampleParty(): JsonResponse
{
$party = new Party();
$party->setName('Sample Party');
$party->setIdeology('Some Ideology');
$party->setLeader('Sample Leader');
$this->entityManager->persist($party);
$this->entityManager->flush();
return $this->json(['message' => 'Sample party added successfully.']);
}
}