Symfony 如何在 twig 中使用控制器功能?

问题描述

我想在 twig 中使用这个 AdminController:

/**
 * @Route("/admin/user/delete",name="deleteUserById")
 */
public function deleteUserById(Request $request): Response
{
    $id = $request->query->get("id");
    $user = $this->getDoctrine()->getRepository(User::class)->find($id);
    $em = $this->getDoctrine()->getManager();
    $em->remove($user);
    $em->flush();
    return $this->redirectToRoute("getAllUsers");
}

twig 文件如下:

{% extends 'base.html.twig' %}

{% block title %}AdminPanel{% endblock %}
{% block stylesheets %}
    <link href="{{ asset('css/global.css') }}" rel="stylesheet"/>
{% endblock %}
{% block nav %}
<div>
    <a class="navbar-brand" href="{{ path('getAllDoctors') }}">Doctors</a>
    <a class="navbar-brand" href="{{ path('getAllApplications') }}">Applications</a>
    <a class="navbar-brand" href="{{ path('getAllPersons') }}">Persons</a>
    <a class="navbar-brand" href="{{ path('getAllPersons') }}">Admin</a>
</div>
{% endblock %}
{% block body %}
<h1>UserID and Email</h1>

<table class="table">
    <thead class="thead-dark">
    <tr>
        <th scope="col">#</th>
        <th scope="col">Email</th>
        <th scope="col">Delete</th>
    </tr>
    </thead>
    {% for user in users %}
        <tbody>
        <form method="post">
        <td> {{ user.id }}</td>
        <td>{{ user.email }}</td>
        <td>
            <button href=" {{ path('deleteUserById',{'POST' : (user.id)}) }}" 
            type="submit">Delete</button>
        </td>
        </form>
        </tbody>
    {% endfor %}
</table>
{% endblock %}

我希望能够使用删除按钮删除用户。但由于某种原因我无法让它工作? 我读到我需要使用表单,但找不到一个很好的例子。

感谢您的帮助。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)