Browse Source

pokemon crud

master
art.dambrine 4 years ago
parent
commit
e174cb301b
  1. 94
      src/Controller/PokemonController.php
  2. 51
      src/Form/PokemonType.php
  3. 4
      templates/pokemon/_delete_form.html.twig
  4. 4
      templates/pokemon/_form.html.twig
  5. 13
      templates/pokemon/edit.html.twig
  6. 49
      templates/pokemon/index.html.twig
  7. 11
      templates/pokemon/new.html.twig
  8. 61
      templates/pokemon/show.html.twig

94
src/Controller/PokemonController.php

@ -0,0 +1,94 @@
<?php
namespace App\Controller;
use App\Entity\Pokemon;
use App\Form\PokemonType;
use App\Repository\PokemonRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/pokemon")
*/
class PokemonController extends AbstractController
{
/**
* @Route("/", name="pokemon_index", methods={"GET"})
*/
public function index(PokemonRepository $pokemonRepository): Response
{
return $this->render('pokemon/index.html.twig', [
'pokemon' => $pokemonRepository->findAll(),
]);
}
/**
* @Route("/new", name="pokemon_new", methods={"GET","POST"})
*/
public function new(Request $request): Response
{
$pokemon = new Pokemon();
$form = $this->createForm(PokemonType::class, $pokemon);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($pokemon);
$entityManager->flush();
return $this->redirectToRoute('pokemon_index');
}
return $this->render('pokemon/new.html.twig', [
'pokemon' => $pokemon,
'form' => $form->createView(),
]);
}
/**
* @Route("/{id}", name="pokemon_show", methods={"GET"})
*/
public function show(Pokemon $pokemon): Response
{
return $this->render('pokemon/show.html.twig', [
'pokemon' => $pokemon,
]);
}
/**
* @Route("/{id}/edit", name="pokemon_edit", methods={"GET","POST"})
*/
public function edit(Request $request, Pokemon $pokemon): Response
{
$form = $this->createForm(PokemonType::class, $pokemon);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('pokemon_index');
}
return $this->render('pokemon/edit.html.twig', [
'pokemon' => $pokemon,
'form' => $form->createView(),
]);
}
/**
* @Route("/{id}", name="pokemon_delete", methods={"POST"})
*/
public function delete(Request $request, Pokemon $pokemon): Response
{
if ($this->isCsrfTokenValid('delete'.$pokemon->getId(), $request->request->get('_token'))) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($pokemon);
$entityManager->flush();
}
return $this->redirectToRoute('pokemon_index');
}
}

51
src/Form/PokemonType.php

@ -0,0 +1,51 @@
<?php
namespace App\Form;
use App\Entity\GenerationPoke;
use App\Entity\Pokemon;
use App\Entity\TypePoke;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class PokemonType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('numero')
->add('nom')
->add('vie')
->add('attaque')
->add('defense')
->add('legendaire')
->add('type1',EntityType::class, array(
'class' => TypePoke::class,
'choice_label' => 'name',
'label' => 'Type 1'
))
->add('type2',EntityType::class, array(
'class' => TypePoke::class,
'required' => false,
'empty_data' => null,
'choice_label' => 'name',
'label' => 'Type 2',
))
->add('generation',EntityType::class, array(
'class' => GenerationPoke::class,
'choice_label' => 'name',
'label' => 'Generation'
))
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Pokemon::class,
]);
}
}

4
templates/pokemon/_delete_form.html.twig

@ -0,0 +1,4 @@
<form method="post" action="{{ path('pokemon_delete', {'id': pokemon.id}) }}" onsubmit="return confirm('Are you sure you want to delete this item?');">
<input type="hidden" name="_token" value="{{ csrf_token('delete' ~ pokemon.id) }}">
<button class="btn">Delete</button>
</form>

4
templates/pokemon/_form.html.twig

@ -0,0 +1,4 @@
{{ form_start(form) }}
{{ form_widget(form) }}
<button class="btn">{{ button_label|default('Save') }}</button>
{{ form_end(form) }}

13
templates/pokemon/edit.html.twig

@ -0,0 +1,13 @@
{% extends 'base.html.twig' %}
{% block title %}Edit Pokemon{% endblock %}
{% block body %}
<h1>Edit Pokemon</h1>
{{ include('pokemon/_form.html.twig', {'button_label': 'Update'}) }}
<a href="{{ path('pokemon_index') }}">back to list</a>
{{ include('pokemon/_delete_form.html.twig') }}
{% endblock %}

49
templates/pokemon/index.html.twig

@ -0,0 +1,49 @@
{% extends 'base.html.twig' %}
{% block title %}Pokemon index{% endblock %}
{% block body %}
<h1>Pokemon index</h1>
<table class="table">
<thead>
<tr>
<th>Numero</th>
<th>Nom</th>
<th>Type 1</th>
<th>Type 2</th>
<th>Generation</th>
<th>Vie</th>
<th>Attaque</th>
<th>Defense</th>
<th>Legendaire</th>
<th>actions</th>
</tr>
</thead>
<tbody>
{% for pokemon in pokemon %}
<tr>
<td>{{ pokemon.numero }}</td>
<td>{{ pokemon.nom }}</td>
<td>{{ pokemon.type1.name }}</td>
<td>{% if pokemon.type2 %} {{ pokemon.type2.name }} {% endif %}</td>
<td>{{ pokemon.generation.name }}</td>
<td>{{ pokemon.vie }}</td>
<td>{{ pokemon.attaque }}</td>
<td>{{ pokemon.defense }}</td>
<td>{{ pokemon.legendaire ? 'Yes' : 'No' }}</td>
<td>
<a href="{{ path('pokemon_show', {'id': pokemon.id}) }}">show</a>
<a href="{{ path('pokemon_edit', {'id': pokemon.id}) }}">edit</a>
</td>
</tr>
{% else %}
<tr>
<td colspan="8">no records found</td>
</tr>
{% endfor %}
</tbody>
</table>
<a href="{{ path('pokemon_new') }}">Create new</a>
{% endblock %}

11
templates/pokemon/new.html.twig

@ -0,0 +1,11 @@
{% extends 'base.html.twig' %}
{% block title %}New Pokemon{% endblock %}
{% block body %}
<h1>Create new Pokemon</h1>
{{ include('pokemon/_form.html.twig') }}
<a href="{{ path('pokemon_index') }}">back to list</a>
{% endblock %}

61
templates/pokemon/show.html.twig

@ -0,0 +1,61 @@
{% extends 'base.html.twig' %}
{% block title %}Pokemon{% endblock %}
{% block body %}
<h1>Pokemon</h1>
<table class="table">
<tbody>
<tr>
<th>Numero</th>
<td>{{ pokemon.numero }}</td>
</tr>
<tr>
<th>Nom</th>
<td>{{ pokemon.nom }}</td>
</tr>
<tr>
<th>Vie</th>
<td>{{ pokemon.vie }}</td>
</tr>
<tr>
<th>Attaque</th>
<td>{{ pokemon.attaque }}</td>
</tr>
<tr>
<th>Defense</th>
<td>{{ pokemon.defense }}</td>
</tr>
<tr>
<th>Legendaire</th>
<td>{{ pokemon.legendaire ? 'Yes' : 'No' }}</td>
</tr>
<tr>
<th>Type 1</th>
<td>{{ pokemon.type1.name }}</td>
</tr>
{% if pokemon.type2 %}
<tr>
<th>Type 2</th>
<td>{{ pokemon.type2.name }}</td>
</tr>
{% endif %}
<tr>
<th>Generation</th>
<td>{{ pokemon.generation.name }}</td>
</tr>
</tbody>
</table>
<a href="{{ path('pokemon_index') }}">back to list</a>
<a href="{{ path('pokemon_edit', {'id': pokemon.id}) }}">edit</a>
{{ include('pokemon/_delete_form.html.twig') }}
{% endblock %}
Loading…
Cancel
Save