未创建/识别路由(Symfony 6.3.3,PHP 8.1)

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

当我使用“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.']);
    }
}
php symfony doctrine
1个回答
0
投票

我通过运行

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.']);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.