I want to use this AdminController in twig:
/**
* @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");
}
The twig file is the following:
{% 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 %}
I want to be able to delete a user with a delete button. But for some reason I can't get it to work? I read that I need to use a form but can't really find a good example.
Thanks for the help.