Removing ToDo Items from the List

Welcome back! In previous lessons, you've already learned how to list, create, and update ToDo items in our Symfony-based MVC application. Now, we will add one final and essential piece of functionality: the ability to delete ToDo items from the list.

Deleting items is an essential feature for any ToDo app because it allows users to manage their tasks more effectively. Imagine having a long list of completed or irrelevant tasks — you want an easy way to remove those items, right? By the end of this lesson, you'll be able to delete any ToDo item from your list.

Defining the Delete Method in the Service Layer
Creating the Delete Endpoint in the Controller
Updating the Twig Template to Include Delete Button

The final step is to update our list.html.twig template to include a "Delete" button for each ToDo item. Twig templates in Symfony are used to render the views of your application.

<!DOCTYPE html>
<html>
<head>
  <title>ToDo List</title>
</head>
<body>
  <h1>ToDo List</h1>
  <ul>
    {% for todo in todos %}
      <li>
        <a href="{{ path('todo_detail', {'id': todo.id}) }}">
          {{ todo.getTitle() }} - {{ todo.getDescription() }}
        </a> 
        <!-- Form for deleting a ToDo item -->
        <form action="{{ path('todo_delete', {'id': todo.id}) }}" method="post" style="display: inline;">
          <button type="submit">Delete</button>
        </form>
      </li>   
    {% else %}
      <li>No todos found</li>
    {% endfor %}
  </ul>
  
  <form action="{{ path('todo_create') }}" method="post">
    <input type="text" name="title" placeholder="Title" required>
    <input type="text" name="description" placeholder="Description">
    <button type="submit">Add To Do</button>
  </form>
</body>
</html>

In this template, for each ToDo item in the list, a form with a Delete button is created. The form's action attribute is set to the delete route using path('todo_delete', {'id': todo.id}). When the Delete button is clicked, the form sends a POST request to the delete endpoint, and the specified ToDo item is deleted. By updating your template, users can now visually interact with the delete functionality.

Example Walkthrough

Let's go through a complete example of deleting a ToDo item.

  1. Click Delete button: Click the "Delete" button next to the desired ToDo item on the list page.
  2. Form submission: The form sends a POST request to the delete endpoint.
  3. Controller processes delete: delete method in TodoController processes the request.
  4. Service deletes item: TodoService removes the ToDo item from the session.
  5. View updated list: You are redirected back to the list view, now without the deleted item.
Recap and Summary

Congratulations! You've successfully added the ability to delete ToDo items in your Symfony-based MVC application. Here's a quick recap of what we've covered:

  • We added a delete method to the TodoService to handle the deletion logic.
  • We created a new route and method in the TodoController to handle delete requests.
  • We updated the list.html.twig template to include a Delete button for each ToDo item.

By completing this lesson, you've built out all the core functionalities of a ToDo app: listing, creating, updating, and deleting items. Now it's time to go to the practice exercises and test your skills further. Keep coding!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal