1

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.

1
  • Just for information: if you want to use PHP code in twig templates,you can make an AppExtension class who extends Twig\Extension\AbstractExtension. In your AppExtension, you create some some functions and the top: you can inject services. Commented Jan 15, 2021 at 9:13

1 Answer 1

3

This is not the best solution, but based on your code you can do like this :

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

twig

{% for user in users %}
    <tbody>
    <form method="post" action="{{ path('deleteUserById', {'id' : user.id}) }}">
    <td> {{ user.id }}</td>
    <td>{{ user.email }}</td>
    <td>
        <button type="submit">Delete</button>
    </td>
    </form>
    </tbody>
{% endfor %}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.