繁体   English   中英

应用程序:使用 symfony 4 的 @ParamConverter 注释未找到发布 object

[英]App:Post object not found by the @ParamConverter annotation using symfony 4

我想通过 id 或 slug 发布,使用 ParamConverter 但我发现应用程序错误: Post object not found by the @ParamConverter annotation object 。知道我是 symfony 的初学者并且我接受了培训。

我试过路线http://127.0.0.1:8000/blog/post/1

BlogController.php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\HttpFoundation\Request;
use App\Entity\Post;
/**
 * @Route("/blog")
*/
class BlogController extends AbstractController {

    /**
    * @Route("/post/{id}", requirements={ "id" : "\d+" }, name="get_one_post_by_id", methods={"GET"})
    * @ParamConverter("post", class="App:Post")
    */
    public function postById($post){
        return $this->json($post);
    }
    /**
    * @Route("/post/{slug}", methods={"GET"})
    * @ParamConverter("post", class="App:Post", options={"mapping": {"slug": "slug"}})
    */
    public function postBySlug($post){
        return $this->json($post);  
    }
    /**
    * @Route("/post/{id}", name="delete-post", methods={"DELETE"})
    */
    public function destroy(Post $post){
        $em = $this->getDoctrine()->getManager();
        $em->remove($post);
        $em->flush();
        return $this->json(null, 204);
    }

它应该看起来像:

use App\Entity\Post;

    /**
    * @Route("/post/{id}", requirements={ "id" : "\d+" }, name="get_one_post_by_id", methods={"GET"})
    */
    public function postById(Post $post){
        return $this->json($post);
    }

就这样

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM