我想通过id或slug发布文章,使用ParamConverter,但我发现App:Post object not found by the @ParamConverter annotation.knowing,我是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);
}发布于 2021-01-07 19:24:52
看起来应该是:
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);
}就这样
https://stackoverflow.com/questions/65599013
复制相似问题