Symfony4 “object not found by the @ParamConverter annotation” 404 error

谁都会走 提交于 2021-01-27 04:07:40

问题


I discover Symfony4 with similar blog sample like describe in https://symfony.com/doc/current/routing.html Then I added a new route to add /blog/about page. So a part of code in my src/Controller/BlogController.php is:

/**
 * @Route("/blog/{id}", name="blog_show")
 */
public function show(Description $article) {
    return $this->render('blog/show.html.twig', [
        'article' => $article,
    ]);
}

/**
 * @Route("blog/about", name="about")
 */
public function about() {
    return $this->render('blog/about.html.twig', [
        'copyright' => "GLPI 3",
    ]);
}

and when I run locahost:8000/blog/about, it returns me a 404 error :
App\Entity\Description object not found by the @ParamConverter annotation


回答1:


If you add a requirement to the route, then the order doesn't matter.

eg.

/**
 * @Route("/blog/{id}", name="blog_show", requirements={"id":"\d+"})
 */

The requirement is a regex.




回答2:


After hours to find solution, I finally read https://symfony.com/doc/current/routing.html and understand that the /blog/{id} annotation catch /blog/about route but can't use it!

By switching functions order in my controller file:

/**
 * @Route("/blog/about", name="blog_about")
 */
public function about() {
    return $this->render('blog/about.html.twig', [
        'copyright' => "GLPI 3",
    ]);
}

/**
 * @Route("/blog/{id}", name="blog_show")
 */
public function show(Description $article) {
    return $this->render('blog/show.html.twig', [
        'article' => $article,
    ]);
}

It works fine !

The solution as mentionned by @tom is the only one with severals entities and controllers !



来源:https://stackoverflow.com/questions/51521607/symfony4-object-not-found-by-the-paramconverter-annotation-404-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!