hola he estado revisando este crud que se genera automaticamente y hay cosas que no entiendo. Por ejemplo, al hacer click en
<a href="{{ path('personas_edit', {'id': persona.id}) }}">edit</a>
¿hace un submit del formulario? Lo pregunto porque probe a eliminar de la ruta el metodo post (dejando solo el get) y me da un error.(No route found for "POST /personas/4/edit": Method Not Allowed (Allow: GET)). Es que parece que pasa el id por get (osea por url) pero en cambio no me funciona. Este es el controlador:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use App\Repository\MyClassRepository;
use App\Entity\Tablaform;
use App\Form\Formulario2Type;
/**
* @Route("/personas")
*/
class ControladorFormularios2Controller extends AbstractController
{
/**
* @Route("/", name="personas_index", methods={"GET"})
*/
public function index(MyClassRepository $myClassRepository): Response
{
return $this->render('controlador_formularios2/index.html.twig', [
'personas' => $myClassRepository->findAll(),
]);
}
/**
* @Route("/new", name="persona_new", methods={"GET","POST"})
*/
public function new(Request $request): Response
{
$tablaForm = new Tablaform();
$form = $this->createForm(Formulario2Type::class, $tablaForm);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($tablaForm);
$entityManager->flush();
return $this->redirectToRoute('personas_index');
}
return $this->render('controlador_formularios2/new.html.twig', [
'tablaForm' => $tablaForm,
'form' => $form->createView(),
]);
}
/**
* @Route("/{id}/delete", name="personas_delete", methods={"GET","POST"})
*/
public function delete(Request $request, Tablaform $tablaForm): Response
{
//if ($this->isCsrfTokenValid('delete'.$tablaForm->getId(), $request->request->get('_token')))
{
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($tablaForm);
$entityManager->flush();
}
return $this->redirectToRoute('personas_index');
}
/**
* @Route("/{id}/edit", name="personas_edit", methods={"GET","POST"})
*/
public function edit(Request $request, Tablaform $tablaForm): Response
{
$form = $this->createForm(Formulario2Type::class, $tablaForm);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('personas_index');
}
return $this->render('controlador_formularios2/edit.html.twig', [
'tablaForm' => $tablaForm,
'form' => $form->createView(),
]);
}
y esta es la vista:
{% extends 'base.html.twig' %}
{% block title %}Listado{% endblock %}
{% block body %}
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Nombre</th>
<th>Edad</th>
</tr>
</thead>
<tbody>
{% for persona in personas %}
<tr>
<td>{{ persona.id }}</td>
<td>{{ persona.nombre }}</td>
<td>{{ persona.edad }}</td>
<td>
{{ include('controlador_formularios2/_delete_form.html.twig') }}
<a href="{{ path('personas_edit', {'id': persona.id}) }}">edit</a>
</td>
</tr>
{% else %}
<tr>
<td colspan="4">no records found</td>
</tr>
{% endfor %}
</tbody>
</table>
<a href="{{ path('persona_new') }}">Create new</a>
{% endblock %}