drh47606 2019-05-15 12:44
浏览 54
已采纳

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

I am in troubles while trying to pass parameters from my form to my controller.

What I am trying to implement is a list of choices that will permit to display a list of users depending on parameters (role, ban ...).

The problem is I think that I don't get the parameter after submit to have my request in index. Could the problem be that I reder the same form in the same function before and after submitting ?

I tried putting href/onclick/ tags on my options but it didn't work.

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,
        ]);
    }
  • 写回答

2条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 写c++代码,第7题说一下使用的计算方法
      • ¥15 有关c++的问题,利用相关知识
      • ¥15 求香农编码和解码的matlab代码
      • ¥20 ROS中的TEB局部规划问题
      • ¥20 关于#matlab#的问题:要求测出上面图片中所有同心圆的半径
      • ¥20 epanet软件运行问题
      • ¥15 Python 文件读取
      • ¥60 dpabi进行Alff计算时脑池有干扰信号
      • ¥15 利用kmeans或kshape聚类分析对归一化的无量纲时间-降雨序列进行聚类
      • ¥15 protel99.SE提示一下弹窗