10 PHP MVC Frameworks Templating and Forms Lab
10 PHP MVC Frameworks Templating and Forms Lab
cd todo_symfony
DATABASE_URL=mysql://root:@127.0.0.1:3306/todo_app?serverVersion=mariadb-
You can create the database directly in your database client interface, or create by Symfony
command line:
Then, you will add more properties as full (category, description, priority, due_date)
listAction
/**
* @Route("/todo", name="todo_list")
*/
public function listAction()
{
$todos = $this->getDoctrine()
->getRepository('App:Todo')
->findAll();
return $this->render('todo/index.html.twig', [
'todos' => $todos
]);
}
This action will dislay all Todos of the table in the database. Before that, you have to create some
Todos in Phpmyadmin
<div class="container">
<div class="row">
<div class="col-md-12">
{% for flash_message in app.session.flashBag.get('notice') %}
<div class="alert alert-success">{{ flash_message }}</div>
{% endfor %}
</body>
</html>
{% block body %}
<div class="bs-example" data-example-id="striped-table">
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>Todo</th>
<th>Due date</th>
<th></th>
</tr>
</thead>
<tbody>
{% for todo in todos %}
<tr>
<th scope="row">{{ todo.id }}</th>
<td>{{ todo.name }}</td>
<td>{{ todo.dueDate|date('F j,Y, g:i a') }}</td>
<td>
{#<a href="{{ path('todo_details', {'id': todo.id}) }} " class='btn btn-success'>View</a>#}
{#<a href="{{ path('todo_edit', {'id': todo.id}) }}" class='btn btn-default'>Edit</a>#}
{#<a href="{{ path('todo_delete', {'id': todo.id}) }}" class='btn btn-danger'>Delete</a>#}
<a href="#" class='btn btn-success'>View</a>
<a href="#" class='btn btn-default'>Edit</a>
<a href="#" class='btn btn-danger'>Delete</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}
Then, run the Server with symfony serve to see the Todo/Index.html.twig page
return $this->render('todo/details.html.twig', [
'todos' => $todos
]);
}
Then, you have to create the view details.html.twig to display information of an specific Todo
details.html.twig
{% extends 'base.html.twig' %}
{% block body %}
<a href="{{ path('todo_list') }}" class="btn btn-default">Back to Todo List</a>
<hr/>
<h2 class="page-header">{{ todos.name }}</h2>
<ul class="list-group">
<li class="list-group-item">Category: {{ todos.category }}</li>
<li class="list-group-item">Priority: {{ todos.priority }}</li>
<li class="list-group-item">Due: <strong>{{ todos.dueDate|date('F j,Y, g:i a') }}</strong></li>
</ul>
<p>{{ todos.description }}</p>
{% endblock %}
In the index.html.twig, you will have to remove and replace following lines
<a href="{{ path('todo_details', {'id': todo.id}) }} " class='btn btn-success'>View</a> (add this line)
$this->addFlash(
'error',
'Todo deleted'
);
return $this->redirectToRoute('todo_list');
}
Then in the index.html.twig, you will remove and replace following lines to use Delete button
<a href="{{ path('todo_delete', {'id': todo.id}) }}" class='btn btn-
danger'>Delete</a>
createAction
Before create a new Todo, you will have to create a FormType by using
return $this->redirectToRoute('todo_list');
}
return $this->render('todo/create.html.twig', [
'form' => $form->createView()
]);
}
return true;
}
return false;
}
In the controller, you will start to implement the editAction($id, Request $request)
/**
* @Route("/todo/edit/{id}", name="todo_edit")
*/
public function editAction($id, Request $request)
{
$em = $this->getDoctrine()->getManager();
$todo = $em->getRepository('App:Todo')->find($id);
return $this->render('todo/edit.html.twig', [
'form' => $form->createView()
]);
}
Then, you will have to create the view edit.html.twig to display the form and submit it to the
controller
edit.html.twig
{% extends 'base.html.twig' %}
{% block body %}
{{ form_start(form) }}
<div class="form-group">
<label for="name">Name:</label>
{{ form_widget(form.name,{
'attr':{
'class':'form-control',
'placeholder': 'Task name'
}
}) }}
</div>
<div class="form-group">
<label for="category">Category:</label>
{{ form_widget(form.category,{
'attr':{
'class':'form-control',
'placeholder': 'Category name'
}
}) }}
</div>
<div class="form-group">
<label for="description">Description:</label>
{{ form_widget(form.description,{
'attr':{
'class':'form-control',
'placeholder': 'Description'
}
}) }}
</div>
<div class="form-group">
<label for="priority">Priority:</label>
{{ form_widget(form.priority,{
Then, you will remove and replace following lines in the index.html.twig to use the Edit button
<a href="#" class='btn btn-default'>Edit</a> (Remove this line)