pin*_* af 5 php symfony-forms symfony symfony-2.8
symfony生成的自动crud操作以及symfony演示应用程序具有以下用于删除操作的代码结构
/**
* Deletes a testing entity.
*
* @Route("/{id}", name="testing_delete")
* @Method("DELETE")
*/
public function deleteAction(Request $request, testing $testing)
{
$form = $this->createDeleteForm($testing);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->remove($testing);
$em->flush();
}
return $this->redirectToRoute('testing_index');
}
/**
* Creates a form to delete a testing entity.
*
* @param testing $testing The testing entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createDeleteForm(testing $testing)
{
return $this->createFormBuilder()
->setAction($this->generateUrl('testing_delete', array('id' => $testing->getId())))
->setMethod('DELETE')
->getForm()
;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是为什么我们需要一个表格来删除?我们只是在树枝上有一个相应的id参数设置链接,不能我们只是做以下,为什么我们需要检查isValid()表格中的实体是否删除它之前?
/**
* test delete
* @Route("/{id}", name="testing_delete")
* @Method("DELETE")
*/
public function deleteAction(testing $testing) {
$em = $this->getDoctrine()->getManager();
$em->remove($testing);
$em->flush();
return $this->redirectToRoute('testing_showall');
}
Run Code Online (Sandbox Code Playgroud)
如果你使用链接删除id,机器人可以通过循环删除你的数据.
在Symfony操作中检查"DELETE"方法以及crsf令牌是否使用方法验证isValid"$ form-> isValid()"
这是安全的原因,它创建形式和验证
| 归档时间: |
|
| 查看次数: |
4311 次 |
| 最近记录: |