邮递员无法正确访问我的 Symfony "PUT" 路由

Postman can't access my Symfony "PUT" Route properly

我看了很多地方,但我无法让我的 PUT 路线发挥作用。我只想让我的 PUT 路由编辑路由中指定的文章的 id,这是我的路由的样子:

/**
 * @return Response
 * @Route("/{id}", methods={"PUT"})
 * @param $id
 * @param Request $request
 */
public function edit($id, Request $request): Response
{
    $em = $this->getDoctrine()->getManager();
    $article = $em->find(Article::class,$id);
    if ($article)
    {
        $request->request->add(["article" => json_decode($request->getContent(), true)]);
        $form = $this->createForm(ArticleType::class, new Article())->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid())
        {
            $article->setNom($form["nom"]->getData());
            $article->setDescription($form["description"]->getData());
            $article->setPrix($form["prix"]->getData());
            $em->refresh($article);
            return $this->json($article, 205);
        }
        return $this->json(["error" => "Bad request"], 400);
    }
    return $this->json(["error" => "Not Found"], 404);
}

这对我来说似乎不错,但对 Postman 来说却不是,尽管它确实进入了代码中,但并不是完全糟糕!但它 returns 400 错误代码,所以我认为这意味着它不会进入第二个 if 并且我不明白为什么,因为我对我的 DELETE 路由使用了类似的设置。我正在尽最大努力学习路由的工作原理,但对于 PUT,即使经过数小时的搜索,我仍然不知所措,因此非常感谢您的帮助!

如果需要,这里是控制器代码的其余部分。是的,我确实告诉我的订阅者允许“PUT”路由。另外,这是我的第一个 post,我希望它足以满足本网站的标准,并感谢所有愿意给我一点时间的人!

    <?php

namespace App\Controller;

use App\Entity\Article;
use App\Form\ArticleType;
use App\Repository\ArticleRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

/**
 * Class ArticleController
 * @package App\Controller
 * @Route("/article")
 */
class ArticleController extends AbstractController
{
    /**
     * @return Response
     * @Route("/", methods={"GET"})
     * @param ArticleRepository $repository
     */
    public function index(ArticleRepository $repository): Response
    {
        return $this->json($repository->findAll());
    }

    /**
     * @return Response
     * @Route("/{id}", methods={"GET"})
     * @param $id
     */
    public function find($id): Response
    {
        $em = $this->getdoctrine()->getmanager();
        $article = $em->find(Article::class,$id);
        if ($article)
        {
            return $this->json($article, 201);
        }
        return $this->json(["error" => "Not Found"], 404);
    }

    /**
     * @return Response
     * @Route("/", methods={"POST"})
     * @param Request $request
     */
    public function new(Request $request): Response
    {
        $request->request->add(["article" => json_decode($request->getContent(), true)]);
        $form = $this->createForm(ArticleType::class, new Article())->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($form->getData());
            $em->flush();
            return $this->json($form->getData(), 201);
        }
        return $this->json(["error" => "Bad request"], 400);
    }

    /**
     * @return Response
     * @Route("/{id}", methods={"DELETE"})
     * @param $id
     */
    public function delete($id): Response
    {
        $em = $this->getdoctrine()->getmanager();
        $article = $em->find(Article::class,$id);
        if ($article)
        {
            $em->remove($article);
            $em->flush();
            return $this->json($article, 205);
        }
        return $this->json(["error" => "Not Found"], 404);
    }

此外,这是我的邮差请求

{
    "nom": "test",
    "description": "description test",
    "prix": 7
}

这里是 ArticleType.php

<?php

namespace App\Form;

use App\Entity\Article;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ArticleType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('nom')
            ->add('description')
            ->add('prix')
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Article::class,
        ]);
    }
}

我有几点建议:

  1. if ($form->isSubmitted() && $form->isValid())分成一些更易读和可调试的:
if ($form->isSubmitted()) {
  if ($form->isValid()) {
    ...
  }
}

这样你可以更好地检查表单是否已提交and/or它是有效的

能否请您粘贴您通过 Postman 发送的请求正文?还有 ArticleType.php class

  1. 永远不要像$request->request->add(["article" => json_decode($request->getContent(), true)]);
  2. 那样修改原始请求

尝试寻找其他方法;你需要这个做什么?

终于成功了,非常感谢大家! 这是最终对我有用的东西:

/**
 * @return Response
 * @Route("/{id}", methods={"PUT"})
 * @param $id
 * @param Request $request
 */
public function edit($id, Request $request): Response
{
    $em = $this->getDoctrine()->getManager();
    $article = $em->find(Article::class,$id);
    if ($article)
    {
        $request->request->add(["article" => json_decode($request->getContent(), true)]);
        $form = $this->createForm(ArticleType::class, $article, array(
            'method' => 'put'
        ))->handleRequest($request);
        if ($form->isSubmitted())
        {
            if ($form->isValid()) {
                $em = $this->getDoctrine()->getManager();
                $em->persist($form->getData());
                $em->flush();
                return $this->json($form->getData(), 201);
            }
            return $this->json(["error" => "on isValid"], 400, array($form->getErrors()));
        }
        return $this->json(["error" => "on isSubmitted"], 400, array($form->getErrors()));
    }
    return $this->json(["error" => "Not Found"], 404);
}

没有'method' => 'put'好像不行..而且我检查过它确实用这个替换了相应ID的数据!非常感谢大家的支持,祝大家有个愉快的一天!!