我发现了类似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)
要求是一个正则表达式。
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 !
| 归档时间: |
|
| 查看次数: |
6304 次 |
| 最近记录: |