Php 是否单击链接<;a href>;在symfony中提交表格?
您好,我一直在审查这个积垢是自动生成的,有一些事情我不明白。例如,单击时:Php 是否单击链接<;a href>;在symfony中提交表格?,php,symfony,twig,Php,Symfony,Twig,您好,我一直在审查这个积垢是自动生成的,有一些事情我不明白。例如,单击时: <a href="{{ path('personas_edit', {'id': persona.id}) }}">edit</a> 你提交表格了吗?我这样问是因为我试图从路由中删除post方法(只留下get),但它给了我一个错误(没有为“post/people/4/edit”找到路由):methodnotallowed(Allow:get))。它似乎通过get(即,通过
<a href="{{ path('personas_edit', {'id': persona.id}) }}">edit</a>
你提交表格了吗?我这样问是因为我试图从路由中删除post方法(只留下get),但它给了我一个错误(没有为“post/people/4/edit”找到路由):methodnotallowed(Allow:get))。它似乎通过get(即,通过url)传递id,但当我在控制器中的edit函数的路由上删除POST方法时,它不起作用。这是控制器:
<?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(),
]);
}
链接本身不能提交表单,但可以通过一些Javascript来提交表单。这段代码中有JS吗?没有,没有javascript。这两种方法都是必需的。如果我删除“post”方法,它也会返回一个错误。我不明白为什么,因为在twig视图中,参数似乎是通过url发送的。这两种方法都存在是有意义的,因为根据控制器判断,该页面有一个表单。因此,您需要GET
打开它,然后POST
处理表单提交。但是链接触发POST
请求是没有意义的。你确定这是唯一发生的事吗?表单在某个时候没有提交?哦,你是对的。edit.html.twig中有一个表单。现在有道理了。非常感谢。
{% 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 %}