Symfony4“ @ParamConverter批注未找到对象” 404错误

bca*_*ag2 3 symfony4

我发现了类似https://symfony.com/doc/current/routing.html中所述的类似博客示例的Symfony4。 然后,我添加了一条新路由来添加/ blog / about页面。因此,我的src / Controller / BlogController.php中的一部分代码是:

/**
 * @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",
    ]);
}
Run Code Online (Sandbox Code Playgroud)

当我运行locahost:8000 / blog / about时,它返回404错误:@ParamConverter批注未找到
App \ Entity \ Description 对象

Tom*_*Tom 13

如果您向路线添加要求,则顺序无关紧要。

例如。

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

要求是一个正则表达式。


bca*_*ag2 7

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,
    ]);
}
Run Code Online (Sandbox Code Playgroud)

It works fine !

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

  • 它可以帮助使用 php bin/console debug:router 或 php bin/console router:match <path> (将 <path> 替换为您的路径,“/about”在我的示例中 (3认同)