Symfony 4 - 如何将参数从表单传递到控制器

我在尝试将参数从我的表单传递到我的控制器时遇到了麻烦。


我试图实现的是一个选项列表,允许根据参数(角色、禁止...)显示用户列表。


问题是我认为提交后我没有得到参数以将我的请求放入索引中。问题可能是我在提交之前和之后在同一个函数中重新编写了同一个表单?


我尝试将 href/onclick/ 标签放在我的选项上,但没有用。


index.html.twig


<form class="form-group" action="{{ path('user_index') }}">

            <table class="table">

                <tr>

                    <td>

                        <select class="custom-select">

                            <option selected="">Which users to you want to see ?</option>

                            <option value="1">All</option>

                            <option value="2">Students</option>

                            <option value="3">Teachers</option>

                            <option value="4">Admins</option>

                            <option value="5">Super Admin</option>

                            <option value="6">Banned</option>

                            <option value="7">Unbanned</option>

                        </select>

                    </td>

                    <td>

                        <input type="submit" value="Submit">

                    </td>

                </tr>

            </table>

        </form>


牛魔王的故事
浏览 127回答 2
2回答

慕哥6287543

据我了解,您的表单的方法是GET请求。要使用 HttpFoundation 组件检索 $_GET 变量,您需要:您的选择输入应该有一个名称。例子:<form class="form-group" action="{{ path('user_index') }}" method="get">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <table class="table">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <select class="custom-select" name="users">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option selected="">Which users to you want to see ?</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="1">All</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="2">Students</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="3">Teachers</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="4">Admins</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="5">Super Admin</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="6">Banned</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="7">Unbanned</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </select>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" value="Submit">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </table></form>尝试使用获取您的请求 ( GET ) 参数$request->query->get('users')

天涯尽头无女友

您的选择没有名称:<select class="custom-select" name="userselect">&nbsp; &nbsp; <option selected="">Which users to you want to see ?</option>&nbsp; &nbsp; <option value="1">All</option>&nbsp; &nbsp; <option value="2">Students</option>&nbsp; &nbsp; <option value="3">Teachers</option>&nbsp; &nbsp; <option value="4">Admins</option>&nbsp; &nbsp; <option value="5">Super Admin</option>&nbsp; &nbsp; <option value="6">Banned</option>&nbsp; &nbsp; <option value="7">Unbanned</option></select>并在您的控制器中获取 $request 中的“userselect”。
打开App,查看更多内容
随时随地看视频慕课网APP