使用 symfony 在注释生根中添加额外变量

Add extra variable in annotation rooting with symfony

我想添加一个额外的变量“type”
所以我在注释中做了多个根。 那我试试这个

/**
 * @Route("/Theme/{id}/{type}", name="theme_show")
 * @Route("/Theme/{id}", name="theme_show")
 */
public function show($id, $type = null, Request $request, ObjectManager $manager){

和return

 return $this->render('blog/show.html.twig', [
                'type' => $type,
            ]);

这是我执行此功能的按钮

<a class="btn btn-dark btn-block" href="{{ path('theme_show', {'id' : theme.id, 'type' : 'question'})}}" role="button">Question</a> 

这是我的 url 当我尝试更改页面时

http://127.0.0.1:8000/Theme/5?type=question

但是当我在这个 url 中转储类型的内容时,我发现 = null

我也尝试使用独特的生根注释

        /**
         * @Route("/Theme/{id}/{type}", name="theme_show")
         */
        public function show($id, $type, Request $request, ObjectManager $manager){

我有这个错误

No route found for "GET /Theme/5" (from "http://127.0.0.1:8000/Theme/5?type=idee")

您使用的 URL 与路线不匹配。

你的URL

http://127.0.0.1:8000/Theme/5?type=question

正确URL

http://127.0.0.1:8000/Theme/5/question

路线

* @Route("/Theme/{id}/{type}", name="theme_show")

如果您想像在 URL 中那样使用查询参数。您必须从请求中检索它。

$type = $request->query->get('type');

接下来,您有两条同名路线。我很确定即使它用于相同的方法也是无效的。如果你 运行 php bin/console debug:router,你会看到只显示最新的。

因此,要么您更改其中一条路线的名称并使用您的模式 /Theme/{id}/{type},要么您使用已解释的查询参数并使用 'type'.

删除路线