尝试将参数从表单传递到控制器时遇到麻烦。
我要实现的是一系列选择,这些选择将允许根据参数(角色,禁令...)显示用户列表。
问题是我认为在提交以使我的请求进入索引后我没有得到参数。 问题可能是我在提交之前和之后在相同功能中重新编辑了相同表格吗?
我尝试将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>
UserController.php
/**
* Requires ADMIN_ROLE
*
* @Route("/", name="user_index", methods={"GET"})
*/
public function index(Request $request): Response
{
$this->denyAccessUnlessGranted('ROLE_ADMIN');
$ban = true;
// trying to get the value of the option in form ?
$value = $request->request->get('value');
switch ($value){
case 1 :
$users = $this->getDoctrine()
->getRepository(User::class)
->findAll();
break;
case 6 :
$users = $this->getDoctrine()
->getRepository(User::class)
->findByBan($ban);
break;
case 7 :
$ban = false;
$users = $this->getDoctrine()
->getRepository(User::class)
->findByBan($ban);
break;
default:
$users = $this->getDoctrine()
->getRepository(User::class)
->findAll();
break;
}
return $this->render('user/index.html.twig', [
'title' => 'User Index',
'headtitle' => 'View all the users list',
'users' => $users,
]);
}
答案 0 :(得分:3)
据我了解,您的表单方法是一个GET
请求。要使用HttpFoundation组件检索$ _GET变量,您需要:
示例:
<form class="form-group" action="{{ path('user_index') }}" method="get">
<table class="table">
<tr>
<td>
<select class="custom-select" name="users">
<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>
$request->query->get('users')
答案 1 :(得分:3)
您选择的名字没有:
<select class="custom-select" name="userselect">
<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>
并在您的控制器中获得$ request中的“ userselect”。