Building A Basic To-Do Application With Symfony 3- Part 3
This is going to be the final part of this tutorial. I have tried my best to make this tutorial as short as possible. In this part fo the tutorial, I will walk you through listing the list of Todos from the database and deleting them also. Enjoy the drive!!! We are almost there
Listing TO-DOs
So, we have been able to create To-Dos and now, we want to be able to list them out in our application. We can do this by following the steps below:
- Update
listAction
method in/src/TodoBundle/Controller/DefaultController.php
file with the below:
/** * @Route("/my/todos", name="todo_list") */ public function listAction() { $repository = $this->getDoctrine()->getRepository('TodoBundle:Todo'); $todos = $repository->findAll(); return $this->render('TodoBundle:Default:list.html.twig',['todos' => $todos]); }
- Also, you will have to update your
list.html.twig
in/src/TodoBundle/Resources/views/Default
folder to:
{% extends 'base.html.twig' %}
{% block body %} <div class="container"> <div class="row"> <div class="col-md-6 col-md-offset-2"> <h1 class="text-center">To do list created successfully</h1> </div> </div> <div class="row"> <div class="col-md-6 col-md-offset-2"> <h3 class="text-center">List To-do Here</h3> {% for todo in todos %} <p>{{ todo.title }}</p> {% endfor %} </div> <a href="{{ path('todo_create') }}" class="btn btn-success btn-md"> Add to ToDo</a> </div> </div>
{% endblock %}
- You can run the Application and add a todo then submit the form. You should see something like this.
Yippee!!! The Last Part: Deleting To-Do
We have gone a long way to this point. I will show you how to delete a todo from the list. What you need to do is follow the following steps.
- Add
deleteAction
method in/src/TodoBundle/Controller/DefaultController.php
file to handle deletes.
/** * @Route("/todo/delete/{id}", name="todo_delete", requirements={"id" = "\d+"}, defaults={"id" = 0}) */ public function deleteAction($id) { $em = $this->getDoctrine()->getManager(); $todo = $em->getRepository('TodoBundle:Todo')->find($id); if (!$todo) { throw $this->createNotFoundException( 'No todo found for id '.$id ); } else { $em->remove($todo); $em->flush(); $this->get('session')->getFlashBag()->add( 'alert', 'Todo Deleted!' ); return $this->redirect($this->generateUrl('todo_list')); } }
- Also, update
list.html.twig
in/src/TodoBundle/Resources/views/Default
folder with:
{% extends 'base.html.twig' %}
{% block body %} <div class="container"> <hr> <div class="row"> <div class="col-md-6 col-md-offset-2"> {% for flashMessage in app.session.flashbag.get('alert') %} <div class="alert alert-success"> <h1 class="text-center"> {{ flashMessage }}</h1> </div> {% endfor %} </div> </div> <div class="row"> <div class="col-md-6 col-md-offset-2"> <h3 class="text-center">List To-do Here</h3> {% for todo in todos %} <p>{{ todo.title }} ==> <a href="{{ path('todo_delete', {'id': todo.id }) }}">Delete</a></p> {% endfor %} </div> <a href="{{ path('todo_create') }}" class="btn btn-success btn-md"> Add to ToDo</a> </div> </div>
{% endblock %}
- Once you delete any To-do, you will get the below.
Run Todo Application
Now, we can create a todo, get a list of Todos created and also delete the Todos we don't want.
Ask Me Any Question About Symfony
Thanks for reading this tutorial. The first part gives an introduction to building a basic To-Do application with Symfony, an awesome PHP web framework while the second and third part of the series, talks about how to create views, work with Twig template engine, how to create bundles entities, Forms and handling requests in Symfony.
Feel Free to ask me question any time or leave a comment for me down here and I will get back to you ASAP.
Your Friend, Goodness Kayode | Pusher Of Codes